Skip to main content

Execute as Python Script

Run marimo notebooks as standard Python scripts from the command line. This is ideal for automation, batch processing, CI/CD pipelines, and workflows that produce side effects like writing to disk or sending notifications.

Basic Execution

Run any marimo notebook as a Python script:
When executed as a script:
  • All cells run in dependency order
  • Outputs go to stdout/stderr
  • UI elements are not interactive
  • The script exits when execution completes
Unlike marimo run, which starts a web server, running as a script executes the notebook once and exits. Perfect for cron jobs, automated reports, and data pipelines.

Why Run as a Script?

Use script execution when:
  • Automating workflows: Scheduled data processing, ETL jobs, report generation
  • CI/CD pipelines: Testing, validation, building artifacts
  • Batch processing: Process files, train models, generate outputs
  • Command-line tools: Interactive CLI applications with argparse
  • System integration: Call from other programs, shell scripts, or schedulers

Command-Line Arguments

Using argparse

The recommended way to handle arguments uses Python’s built-in argparse:
Run it:

Using simple-parsing

For more complex configurations, use simple-parsing:
Run it:

Using mo.cli_args()

marimo provides a lightweight argument parser:
Run it:
mo.cli_args() does basic type inference but doesn’t provide argument validation or help text. For production scripts, use argparse or simple-parsing.

Parameterization Patterns

Environment Variables

Use environment variables for configuration:
Run it:

Configuration Files

Load parameters from JSON, YAML, or TOML:
Run it:

Conditional Logic

Detect execution mode and adapt behavior:

Output and Side Effects

Writing Files

Console Output

Print statements appear in the terminal:
Output:

Exit Codes

Return meaningful exit codes for automation:
Check exit codes in bash:

Integration with Workflows

Cron Jobs

Schedule regular execution:

GitHub Actions

Integrate with CI/CD:

Shell Scripts

Orchestrate multiple notebooks:

Python Subprocess

Call from other Python code:

Validation Before Execution

Check notebooks for issues before running:
The marimo check command validates:
  • Multiple definition errors
  • Delete-nonlocal errors
  • Cycles in the dependency graph
  • Other common issues
See the Lint Rules guide for details.

Export with Execution

Combine execution with export to HTML:
This runs the notebook and captures all outputs in the HTML file.

Performance Considerations

Optimize script execution:
  1. Cache expensive computations: Use @functools.cache or persist results to disk
  2. Process in chunks: For large datasets, use batch processing
  3. Parallelize: Use multiprocessing or joblib for CPU-bound tasks
  4. Profile first: Use python -m cProfile script.py to identify bottlenecks
  5. Minimize dependencies: Import only what’s needed in each cell

Debugging Scripts

Use Python’s debugger:

Examples

Daily Report Generator

Run daily:

Model Training Pipeline

Data Validation

Best Practices

For production scripts:
  1. Use argparse for clear argument definitions and help text
  2. Validate inputs before processing
  3. Handle errors gracefully with try/except
  4. Log important events to files or monitoring systems
  5. Return meaningful exit codes (0 for success, non-zero for errors)
  6. Make scripts idempotent so they can safely re-run
  7. Test in notebook mode first before deploying as script

Next Steps

Deploy as App

Run notebooks as interactive web applications

CLI Arguments

Advanced command-line argument handling

Export Formats

Export notebooks to HTML, PDF, and more

CI/CD Integration

Deploy scripts in automated workflows