Commit ·
e9aa104
1
Parent(s): 55b755d
Simplify getting-started notebook
Browse files- Cleaner pattern: mo.md() for docs, print() for runtime output
- Interactive UI controls that fall back to CLI args
- Works as both tutorial and script
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- getting-started.py +83 -70
getting-started.py
CHANGED
|
@@ -7,13 +7,13 @@
|
|
| 7 |
# ]
|
| 8 |
# ///
|
| 9 |
"""
|
| 10 |
-
Getting Started with
|
| 11 |
|
| 12 |
-
This
|
| 13 |
-
- Interactive
|
| 14 |
-
-
|
| 15 |
|
| 16 |
-
|
| 17 |
"""
|
| 18 |
|
| 19 |
import marimo
|
|
@@ -24,101 +24,118 @@ app = marimo.App(width="medium")
|
|
| 24 |
@app.cell
|
| 25 |
def _():
|
| 26 |
import marimo as mo
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
| 28 |
mo.md(
|
| 29 |
"""
|
| 30 |
-
# Getting Started with
|
| 31 |
|
| 32 |
-
This notebook shows how to
|
| 33 |
|
| 34 |
-
**
|
| 35 |
-
-
|
| 36 |
-
-
|
| 37 |
"""
|
| 38 |
)
|
| 39 |
-
return
|
| 40 |
|
| 41 |
|
| 42 |
@app.cell
|
| 43 |
def _(mo):
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
parser.add_argument(
|
| 51 |
-
"--dataset",
|
| 52 |
-
default="stanfordnlp/imdb",
|
| 53 |
-
help="Dataset to load (default: stanfordnlp/imdb)",
|
| 54 |
-
)
|
| 55 |
-
parser.add_argument(
|
| 56 |
-
"--split",
|
| 57 |
-
default="train",
|
| 58 |
-
help="Split to load (default: train)",
|
| 59 |
-
)
|
| 60 |
-
parser.add_argument(
|
| 61 |
-
"--num-samples",
|
| 62 |
-
type=int,
|
| 63 |
-
default=5,
|
| 64 |
-
help="Number of samples to display (default: 5)",
|
| 65 |
)
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
args, _ = parser.parse_known_args()
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
mo.md(
|
| 71 |
-
|
| 72 |
-
##
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
- **Samples to show**: `{args.num_samples}`
|
| 77 |
"""
|
| 78 |
)
|
| 79 |
-
return
|
| 80 |
|
| 81 |
|
| 82 |
@app.cell
|
| 83 |
-
def _(
|
| 84 |
from datasets import load_dataset
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
@app.cell
|
| 91 |
-
def _(
|
| 92 |
-
# Load the dataset
|
| 93 |
-
dataset = load_dataset(args.dataset, split=args.split)
|
| 94 |
-
|
| 95 |
mo.md(
|
| 96 |
-
|
| 97 |
-
##
|
| 98 |
|
| 99 |
-
|
| 100 |
-
- **Split**: {args.split}
|
| 101 |
-
- **Number of rows**: {len(dataset):,}
|
| 102 |
-
- **Features**: {list(dataset.features.keys())}
|
| 103 |
"""
|
| 104 |
)
|
| 105 |
-
return
|
| 106 |
|
| 107 |
|
| 108 |
@app.cell
|
| 109 |
-
def _(
|
| 110 |
-
#
|
| 111 |
-
samples = dataset.select(range(min(
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
# Display as a table (marimo will render this nicely)
|
| 120 |
-
samples.to_pandas()
|
| 121 |
-
return
|
| 122 |
|
| 123 |
|
| 124 |
@app.cell
|
|
@@ -127,13 +144,9 @@ def _(mo):
|
|
| 127 |
"""
|
| 128 |
## Next Steps
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
2. **Explore interactively**: Edit cells, add visualizations
|
| 134 |
-
3. **Run as batch**: `uv run getting-started.py --dataset squad --num-samples 10`
|
| 135 |
-
|
| 136 |
-
Check out more UV scripts at [uv-scripts on Hugging Face](https://huggingface.co/uv-scripts)
|
| 137 |
"""
|
| 138 |
)
|
| 139 |
return
|
|
|
|
| 7 |
# ]
|
| 8 |
# ///
|
| 9 |
"""
|
| 10 |
+
Getting Started with Hugging Face Datasets
|
| 11 |
|
| 12 |
+
This marimo notebook works in two modes:
|
| 13 |
+
- Interactive: uvx marimo edit --sandbox getting-started.py
|
| 14 |
+
- Script: uv run getting-started.py --dataset squad
|
| 15 |
|
| 16 |
+
Same file, two experiences.
|
| 17 |
"""
|
| 18 |
|
| 19 |
import marimo
|
|
|
|
| 24 |
@app.cell
|
| 25 |
def _():
|
| 26 |
import marimo as mo
|
| 27 |
+
return (mo,)
|
| 28 |
+
|
| 29 |
|
| 30 |
+
@app.cell
|
| 31 |
+
def _(mo):
|
| 32 |
mo.md(
|
| 33 |
"""
|
| 34 |
+
# Getting Started with Hugging Face Datasets
|
| 35 |
|
| 36 |
+
This notebook shows how to load and explore datasets from the Hugging Face Hub.
|
| 37 |
|
| 38 |
+
**Run this notebook:**
|
| 39 |
+
- Interactive: `uvx marimo edit --sandbox getting-started.py`
|
| 40 |
+
- As a script: `uv run getting-started.py --dataset squad`
|
| 41 |
"""
|
| 42 |
)
|
| 43 |
+
return
|
| 44 |
|
| 45 |
|
| 46 |
@app.cell
|
| 47 |
def _(mo):
|
| 48 |
+
mo.md(
|
| 49 |
+
"""
|
| 50 |
+
## Step 1: Configure
|
| 51 |
|
| 52 |
+
Choose which dataset to load. In interactive mode, use the controls below.
|
| 53 |
+
In script mode, pass `--dataset` argument.
|
| 54 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
+
return
|
| 57 |
+
|
| 58 |
|
| 59 |
+
@app.cell
|
| 60 |
+
def _(mo):
|
| 61 |
+
import argparse
|
| 62 |
+
|
| 63 |
+
# Parse CLI args (works in both modes)
|
| 64 |
+
parser = argparse.ArgumentParser()
|
| 65 |
+
parser.add_argument("--dataset", default="stanfordnlp/imdb")
|
| 66 |
+
parser.add_argument("--split", default="train")
|
| 67 |
+
parser.add_argument("--samples", type=int, default=5)
|
| 68 |
args, _ = parser.parse_known_args()
|
| 69 |
|
| 70 |
+
# Interactive controls (only shown in notebook mode)
|
| 71 |
+
dataset_input = mo.ui.text(value=args.dataset, label="Dataset")
|
| 72 |
+
split_input = mo.ui.dropdown(["train", "test", "validation"], value=args.split, label="Split")
|
| 73 |
+
samples_input = mo.ui.slider(1, 20, value=args.samples, label="Samples")
|
| 74 |
+
|
| 75 |
+
mo.hstack([dataset_input, split_input, samples_input])
|
| 76 |
+
return args, argparse, dataset_input, parser, samples_input, split_input
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
@app.cell
|
| 80 |
+
def _(args, dataset_input, mo, samples_input, split_input):
|
| 81 |
+
# Use interactive values if available, otherwise CLI args
|
| 82 |
+
dataset_name = dataset_input.value or args.dataset
|
| 83 |
+
split_name = split_input.value or args.split
|
| 84 |
+
num_samples = samples_input.value or args.samples
|
| 85 |
+
|
| 86 |
+
print(f"Dataset: {dataset_name}, Split: {split_name}, Samples: {num_samples}")
|
| 87 |
+
return dataset_name, num_samples, split_name
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
@app.cell
|
| 91 |
+
def _(mo):
|
| 92 |
mo.md(
|
| 93 |
+
"""
|
| 94 |
+
## Step 2: Load Dataset
|
| 95 |
|
| 96 |
+
We use the `datasets` library to stream data directly from the Hub.
|
| 97 |
+
No need to download the entire dataset first!
|
|
|
|
| 98 |
"""
|
| 99 |
)
|
| 100 |
+
return
|
| 101 |
|
| 102 |
|
| 103 |
@app.cell
|
| 104 |
+
def _(dataset_name, split_name):
|
| 105 |
from datasets import load_dataset
|
| 106 |
|
| 107 |
+
print(f"Loading {dataset_name}...")
|
| 108 |
+
dataset = load_dataset(dataset_name, split=split_name)
|
| 109 |
+
print(f"Loaded {len(dataset):,} rows")
|
| 110 |
+
print(f"Features: {list(dataset.features.keys())}")
|
| 111 |
+
return dataset, load_dataset
|
| 112 |
|
| 113 |
|
| 114 |
@app.cell
|
| 115 |
+
def _(mo):
|
|
|
|
|
|
|
|
|
|
| 116 |
mo.md(
|
| 117 |
+
"""
|
| 118 |
+
## Step 3: Explore the Data
|
| 119 |
|
| 120 |
+
Let's look at a few samples from the dataset.
|
|
|
|
|
|
|
|
|
|
| 121 |
"""
|
| 122 |
)
|
| 123 |
+
return
|
| 124 |
|
| 125 |
|
| 126 |
@app.cell
|
| 127 |
+
def _(dataset, mo, num_samples):
|
| 128 |
+
# Select samples and display
|
| 129 |
+
samples = dataset.select(range(min(num_samples, len(dataset))))
|
| 130 |
+
df = samples.to_pandas()
|
| 131 |
|
| 132 |
+
# Truncate long text for display
|
| 133 |
+
for col in df.select_dtypes(include=["object"]).columns:
|
| 134 |
+
df[col] = df[col].apply(lambda x: str(x)[:200] + "..." if len(str(x)) > 200 else x)
|
| 135 |
|
| 136 |
+
print(df.to_string()) # Shows in script mode
|
| 137 |
+
mo.ui.table(df) # Shows in interactive mode
|
| 138 |
+
return df, samples
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
|
| 141 |
@app.cell
|
|
|
|
| 144 |
"""
|
| 145 |
## Next Steps
|
| 146 |
|
| 147 |
+
- Try different datasets: `squad`, `emotion`, `wikitext`
|
| 148 |
+
- Run on HF Jobs: `hf jobs uv run --flavor cpu-basic ... getting-started.py`
|
| 149 |
+
- Check out more UV scripts at [uv-scripts](https://huggingface.co/uv-scripts)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
"""
|
| 151 |
)
|
| 152 |
return
|