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:- 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-inargparse:
Using simple-parsing
For more complex configurations, usesimple-parsing:
Using mo.cli_args()
marimo provides a lightweight argument parser:Parameterization Patterns
Environment Variables
Use environment variables for configuration:Configuration Files
Load parameters from JSON, YAML, or TOML:Conditional Logic
Detect execution mode and adapt behavior:Output and Side Effects
Writing Files
Console Output
Print statements appear in the terminal:Exit Codes
Return meaningful exit codes for automation: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:marimo check command validates:
- Multiple definition errors
- Delete-nonlocal errors
- Cycles in the dependency graph
- Other common issues
Export with Execution
Combine execution with export to HTML:Performance Considerations
Debugging Scripts
Use Python’s debugger:Examples
Daily Report Generator
Model Training Pipeline
Data Validation
Best Practices
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