File size: 4,140 Bytes
55b755d 1160c58 55b755d 1160c58 55b755d 1160c58 55b755d 1160c58 55b755d 1160c58 55b755d 1160c58 55b755d 1160c58 55b755d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
---
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)
|