| # /// script | |
| # requires-python = ">=3.10" | |
| # dependencies = [ | |
| # "marimo", | |
| # # Add your dependencies here, e.g.: | |
| # # "datasets", | |
| # # "transformers", | |
| # # "torch", | |
| # ] | |
| # /// | |
| """ | |
| Your Notebook Title | |
| Brief description of what this notebook does. | |
| Two ways to run: | |
| - Tutorial: uvx marimo edit --sandbox your-notebook.py | |
| - Script: uv run your-notebook.py --your-args | |
| """ | |
| import marimo | |
| app = marimo.App(width="medium") | |
| # ============================================================================= | |
| # Cell 1: Import marimo | |
| # This cell is required - it imports marimo for use in other cells | |
| # ============================================================================= | |
| def _(): | |
| import marimo as mo | |
| return (mo,) | |
| # ============================================================================= | |
| # Cell 2: Introduction (notebook mode only) | |
| # Use mo.md() for explanations that only show in interactive mode | |
| # ============================================================================= | |
| def _(mo): | |
| mo.md( | |
| """ | |
| # Your Notebook Title | |
| Explain what this notebook does and why it's useful. | |
| **Two ways to run:** | |
| - **Tutorial**: `uvx marimo edit --sandbox your-notebook.py` | |
| - **Script**: `uv run your-notebook.py --your-args` | |
| """ | |
| ) | |
| return | |
| # ============================================================================= | |
| # Cell 3: Configuration | |
| # Pattern: argparse for CLI + mo.ui for interactive controls | |
| # Interactive controls fall back to CLI defaults | |
| # ============================================================================= | |
| def _(mo): | |
| import argparse | |
| # Parse CLI args (works in both modes) | |
| parser = argparse.ArgumentParser(description="Your script description") | |
| parser.add_argument("--input", default="default_value", help="Input parameter") | |
| parser.add_argument("--count", type=int, default=10, help="Number of items") | |
| args, _ = parser.parse_known_args() | |
| # Interactive controls (shown in notebook mode) | |
| # These use CLI args as defaults, so script mode still works | |
| input_control = mo.ui.text(value=args.input, label="Input") | |
| count_control = mo.ui.slider(1, 100, value=args.count, label="Count") | |
| mo.hstack([input_control, count_control]) | |
| return argparse, args, count_control, input_control, parser | |
| # ============================================================================= | |
| # Cell 4: Resolve values | |
| # Use interactive values if set, otherwise fall back to CLI args | |
| # print() shows output in BOTH modes (script stdout + notebook console) | |
| # ============================================================================= | |
| def _(args, count_control, input_control): | |
| # Resolve values (interactive takes precedence) | |
| input_value = input_control.value or args.input | |
| count_value = count_control.value or args.count | |
| # print() works in both modes - shows in terminal for scripts, | |
| # shows in cell output for notebooks | |
| print(f"Input: {input_value}") | |
| print(f"Count: {count_value}") | |
| return count_value, input_value | |
| # ============================================================================= | |
| # Cell 5: Your main logic | |
| # This is where you do the actual work | |
| # ============================================================================= | |
| def _(count_value, input_value, mo): | |
| mo.md( | |
| """ | |
| ## Processing | |
| Explain what this step does... | |
| """ | |
| ) | |
| # Your processing code here | |
| results = [f"{input_value}_{i}" for i in range(count_value)] | |
| print(f"Generated {len(results)} results") | |
| return (results,) | |
| # ============================================================================= | |
| # Cell 6: Display results | |
| # Use mo.md() or mo.ui.table() for rich display in notebook mode | |
| # Use print() for output that shows in both modes | |
| # ============================================================================= | |
| def _(mo, results): | |
| # Show in notebook mode (rich display) | |
| mo.md("### Results\n\n- " + "\n- ".join(results[:5]) + "\n- ...") | |
| # Also print for script mode | |
| print(f"First 5 results: {results[:5]}") | |
| return | |
| # ============================================================================= | |
| # Entry point - required for script mode | |
| # ============================================================================= | |
| if __name__ == "__main__": | |
| app.run() | |