| --- |
| viewer: false |
| tags: |
| - uv-script |
| - marimo |
| - tutorial |
| --- |
| |
| # Marimo UV Scripts |
|
|
| [Marimo](https://marimo.io/) notebooks that work as both **interactive tutorials** and **batch scripts**. |
|
|
| ## What is this? |
|
|
| Marimo notebooks are pure Python files that can be: |
| - **Edited interactively** with a reactive notebook interface |
| - **Run as scripts** with `uv run` - same as any UV script |
|
|
| This makes them perfect for tutorials and educational content where you want users to explore step-by-step, but also run the whole thing as a batch job. |
|
|
| ## Available Scripts |
|
|
| | Script | Description | |
| |--------|-------------| |
| | `getting-started.py` | Introduction to UV scripts and HF datasets | |
| | `train-image-classifier.py` | Fine-tune a Vision Transformer on image classification | |
| | `_template.py` | Minimal template for creating your own notebooks | |
|
|
| ## Usage |
|
|
| ### Run as a script |
|
|
| ```bash |
| # Get dataset info |
| uv run https://huggingface.co/datasets/uv-scripts/marimo/raw/main/getting-started.py --dataset squad |
| |
| # Train an image classifier |
| uv run https://huggingface.co/datasets/uv-scripts/marimo/raw/main/train-image-classifier.py \ |
| --dataset beans \ |
| --epochs 3 \ |
| --output-repo your-username/beans-vit |
| ``` |
|
|
| ### Run interactively |
|
|
| ```bash |
| # Clone and edit locally |
| git clone https://huggingface.co/datasets/uv-scripts/marimo |
| cd marimo |
| |
| # Open in marimo editor (--sandbox auto-installs dependencies) |
| uvx marimo edit --sandbox getting-started.py |
| uvx marimo edit --sandbox train-image-classifier.py |
| ``` |
|
|
| ### Run on HF Jobs (GPU) |
|
|
| ```bash |
| # Train image classifier with GPU |
| hf jobs uv run --flavor l4x1 --secrets HF_TOKEN \ |
| https://huggingface.co/datasets/uv-scripts/marimo/raw/main/train-image-classifier.py \ |
| -- --dataset beans --output-repo your-username/beans-vit --epochs 5 --push-to-hub |
| ``` |
|
|
| ## Why Marimo? |
|
|
| - **Reactive**: Cells automatically re-run when dependencies change |
| - **Pure Python**: No JSON, git-friendly, readable as plain code |
| - **Self-contained**: Inline dependencies via PEP 723 metadata |
| - **Dual-mode**: Same file works as notebook and script |
|
|
| ## Create Your Own Marimo UV Script |
|
|
| Use `_template.py` as a starting point: |
|
|
| ```bash |
| # Clone and copy the template |
| git clone https://huggingface.co/datasets/uv-scripts/marimo |
| cp marimo/_template.py my-notebook.py |
| |
| # Edit interactively |
| uvx marimo edit --sandbox my-notebook.py |
| |
| # Test as script |
| uv run my-notebook.py --help |
| ``` |
|
|
| ## Recipes |
|
|
| ### Add explanation (notebook only) |
|
|
| ```python |
| mo.md(""" |
| ## This is a heading |
| |
| This text explains what's happening. Only shows in interactive mode. |
| """) |
| ``` |
|
|
| ### Show output in both modes |
|
|
| ```python |
| # print() shows in terminal (script) AND cell output (notebook) |
| print(f"Loaded {len(data)} items") |
| ``` |
|
|
| ### Interactive control with CLI fallback |
|
|
| ```python |
| # Parse CLI args first |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--count", type=int, default=10) |
| args, _ = parser.parse_known_args() |
| |
| # Create UI control with CLI default |
| slider = mo.ui.slider(1, 100, value=args.count, label="Count") |
| |
| # Use it - works in both modes |
| count = slider.value # UI value in notebook, CLI value in script |
| ``` |
|
|
| ### Show visuals (notebook only) |
|
|
| ```python |
| # mo.md() with images, mo.ui.table(), etc. only display in notebook |
| mo.ui.table(dataframe) |
| |
| # For script mode, also print summary |
| print(f"DataFrame has {len(df)} rows") |
| ``` |
|
|
| ### Conditional notebook-only code |
|
|
| ```python |
| # Check if running interactively |
| if hasattr(mo, 'running_in_notebook') and mo.running_in_notebook(): |
| # Heavy visualization only in notebook |
| show_complex_plot(data) |
| ``` |
|
|
| ## Best Practices |
|
|
| 1. **Always include `print()` for important output** - It works in both modes |
| 2. **Use argparse for all configuration** - CLI args work everywhere |
| 3. **Add `mo.md()` explanations between steps** - Makes tutorials readable |
| 4. **Test in script mode first** - Ensure it works without interactivity |
| 5. **Keep dependencies minimal** - Add `marimo` plus only what you need |
|
|
| ## Learn More |
|
|
| - [Marimo documentation](https://docs.marimo.io/) |
| - [UV scripts guide](https://docs.astral.sh/uv/guides/scripts/) |
| - [uv-scripts organization](https://huggingface.co/uv-scripts) |
|
|