Skip to main content

IPython magic command for embedding marimo notebooks in Jupyter/Colab

Project description

🧬 Marimo Magic for Jupyter/Colab

Python 3.8+ License: MIT Jupyter Google Colab

A custom IPython magic command that embeds marimo notebooks directly in Jupyter notebooks and Google Colab, similar to %tensorboard. Perfect for interactive development, presentations, and sharing reactive Python notebooks.

✨ Features

  • 🔄 UV Integration: Automatic dependency management with uv support
  • 🛡️ Colab Compatibility: Seamless integration with Google Colab's iframe restrictions
  • 📝 Edit & Run Modes: Full development in edit mode, presentations in run mode
  • 🎯 Smart Fallbacks: Robust iframe embedding with direct link alternatives
  • 🔧 Advanced Debugging: Comprehensive diagnostics and connection testing
  • 📦 Inline Dependencies: Supports PEP 723 script dependencies out of the box

🚀 Quick Start

Installation

Option 1: Copy files directly (recommended for Colab)

  1. Download magic.py and __init__.py from this repository
  2. Upload to your Jupyter/Colab session
  3. Load the extension:
%load_ext marimo_magic

Option 2: Install via pip (when package is published)

pip install marimo-magic

Basic Usage

# Load the extension
%load_ext marimo_magic

# Start marimo with default settings (creates temp notebook in run mode)
%marimo

# Run an existing notebook
%marimo my_notebook.py

# Edit mode for development
%marimo my_notebook.py --edit

# Custom dimensions and port
%marimo my_notebook.py --port 8080 --height 800 --width 90%

📖 Usage Guide

Run Mode vs Edit Mode

Mode Purpose Features Use Cases
Run (default) Interactive execution View outputs, interact with widgets Demos, presentations, exploration
Edit Development Add/edit/delete cells, full IDE Notebook development, debugging
# Development workflow
%marimo analysis.py --edit --debug

# Presentation mode  
%marimo analysis.py --height 900

# Quick exploration with temporary notebook
%marimo --edit

Command Options

Option Description Example
notebook_path Path to marimo notebook file %marimo my_notebook.py
--edit Run in edit mode %marimo --edit
--port PORT Specify server port %marimo --port 8080
--height HEIGHT Iframe height in pixels %marimo --height 800
--width WIDTH Iframe width %marimo --width 80%
--debug Enable debug output %marimo --debug
--stop PORT Stop server on port %marimo --stop 8080
--diagnose Run diagnostics %marimo --diagnose

Inline Dependencies (PEP 723)

Your marimo notebooks can include dependencies directly in the script:

# /// script
# requires-python = ">=3.8"
# dependencies = [
#     "marimo",
#     "torch>=2.0.0",
#     "transformers>=4.20.0",
#     "pandas",
#     "matplotlib",
# ]
# ///

import marimo

__generated_with = "0.14.9"
app = marimo.App()

@app.cell
def __():
    import torch
    import transformers
    # Your code here...

The magic command automatically detects inline dependencies and:

  • Uses uv run marimo when available for isolated environments
  • Adds --sandbox flag to avoid terminal interactions
  • Extends timeout for package installation
  • Shows progress indicators for heavy packages

🛠️ Troubleshooting

Debug Mode

%marimo my_notebook.py --debug

Connection Testing

%marimo --diagnose

This runs comprehensive checks:

  • ✅ UV and marimo installation
  • ✅ Port availability
  • ✅ Server startup testing
  • ✅ Environment detection (Colab vs Jupyter)

Common Issues

Issue Solution
Iframe appears empty in Colab Click "Open Marimo Notebook" button
Server won't start Check %marimo --diagnose
Dependencies not installing Ensure uv is available or install packages manually
Edit mode timeout Files with heavy dependencies need 3-5 minutes

🏗️ Architecture

The magic command:

  1. Detects environment: Colab vs regular Jupyter
  2. Chooses runner: uv run marimo vs python -m marimo
  3. Starts server: Headless mode with auto-port selection
  4. Handles dependencies: Automatic sandbox mode for PEP 723 scripts
  5. Creates display: Smart iframe embedding with fallbacks

Google Colab Integration

Special handling for Colab's security restrictions:

  • Uses google.colab.output.eval_js() for proper proxy URLs
  • Provides iframe + direct link fallbacks
  • Auto-opens in new tab when iframe fails
  • Handles authentication bypass with --no-token

📝 Example Workflows

Data Science Notebook

# Create a new analysis notebook
%marimo analysis.py --edit

# Present results
%marimo analysis.py --height 900

# Share with colleagues (generates shareable link)
%marimo analysis.py --port 8080

ML Model Development

# Work on model with heavy dependencies
%marimo model_training.py --edit --debug

# Quick inference demo
%marimo model_inference.py

# Stop all servers
%marimo --stop 8080
%marimo --stop 2718

Interactive Teaching

# Student exploration mode
%marimo --edit --height 700

# Instructor presentation mode
%marimo lesson_notebook.py --width 100%

🤝 Contributing

Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.

Development Setup

git clone https://github.com/hublot-io/marimo-magic.git
cd marimo-magic
pip install -e .

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • marimo - The reactive Python notebook framework
  • Jupyter - The foundation for interactive computing
  • Google Colab - Free GPU access for everyone

🔗 Related Projects

  • marimo - The main marimo project
  • uv - Fast Python package manager
  • Jupyter - The Jupyter ecosystem

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marimo_magic-1.0.0.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

marimo_magic-1.0.0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file marimo_magic-1.0.0.tar.gz.

File metadata

  • Download URL: marimo_magic-1.0.0.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for marimo_magic-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5a7e7eecad38a5c137bfa8b2ad1b5ce4d1d72ffbd83dbdd5d4f41fc8302b3b8d
MD5 dce8ec931a1133aa61e3d6c79c1733f7
BLAKE2b-256 8f7f7389a45a440562946737a367ce3983851c726cee068196580718fdd1dabb

See more details on using hashes here.

File details

Details for the file marimo_magic-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: marimo_magic-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for marimo_magic-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8886dbc6cfae7b3c3c3bd8abd07354ba99030e2e08223f822f52f3557a8e3c02
MD5 3e368491cc2878ed2b65bf1a0f4dda0a
BLAKE2b-256 73941f57216bc53f07bf66d264f2835c661be3e76b0da781a5d5ed17218ad0c2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page