File size: 4,444 Bytes
1160c58 |
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 |
# /// 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
# =============================================================================
@app.cell
def _():
import marimo as mo
return (mo,)
# =============================================================================
# Cell 2: Introduction (notebook mode only)
# Use mo.md() for explanations that only show in interactive mode
# =============================================================================
@app.cell
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
# =============================================================================
@app.cell
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)
# =============================================================================
@app.cell
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
# =============================================================================
@app.cell
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
# =============================================================================
@app.cell
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()
|