Working with Expensive Notebooks
marimo’s reactive execution automatically runs cells when their dependencies change. For notebooks with expensive computations, you can configure marimo to use lazy execution mode, which marks cells as stale instead of automatically running them.Lazy Execution Mode
Lazy execution gives you the benefits of reactive programming (tracking dependencies and detecting stale cells) while preventing accidental execution of expensive operations.Configuring Lazy Mode
Execution Modes
marimo supports two execution modes:| Mode | Behavior | Use Case |
|---|---|---|
"autorun" | Cells automatically run when dependencies change | Default, interactive exploration |
"lazy" | Cells are marked as stale, require manual execution | Expensive computations, ETL pipelines |
Runtime Configuration Options
Frommarimo/_config/config.py:
Key Settings for Expensive Notebooks
auto_instantiate
Control whether cells run automatically when opening a notebook:This only applies in edit mode. Apps always run automatically.
on_cell_change
Control how cells react to changes:auto_reload
Control behavior when imported modules change:Controlling Cell Execution
Stale Cell Indicators
In lazy mode, marimo marks cells as stale with visual indicators:- Yellow dot: Cell’s dependencies have changed
- Run button: Click to execute stale cells
- Run All Stale: Batch execute all stale cells
Manual Execution Strategies
Run individual cells as needed
Run individual cells as needed
Click the run button on specific stale cells when you need their results:
Run All Stale Cells
Run All Stale Cells
Use the “Run All Stale” button to execute all stale cells in dependency order:
- Keyboard shortcut:
Cmd/Ctrl + Shift + Enter - Menu: Runtime → Run stale cells
Disable cells temporarily
Disable cells temporarily
Disable expensive cells to prevent them from running:
Performance Optimization Strategies
1. Caching Expensive Computations
2. Lazy Data Loading
3. Incremental Processing
4. Conditional Execution
Output Size Management
Limit output sizes to prevent frontend performance issues:Execution Type: Strict vs Relaxed
marimo offers two execution types for different memory management strategies:relaxed: Faster, shares objects between cells (default)strict: Clones cell outputs to prevent hidden state accumulation
Working with Module Reloading
When developing modules imported by your notebook:"off": Never reload modules"lazy": Mark cells importing modified modules as stale"autorun": Automatically re-run cells when modules change
marimo uses intelligent code analysis to track module dependencies, similar to IPython’s
%autoreload but with better integration.Example: ETL Pipeline
Complete example of configuring a notebook with expensive operations:Best Practices
Use lazy mode for data pipelines
Use lazy mode for data pipelines
ETL notebooks benefit from manual control over execution flow:
Combine with mo.stop()
Combine with mo.stop()
Add runtime controls for conditional execution:
Profile before optimizing
Profile before optimizing
Use marimo’s cell timing to identify bottlenecks:
- Cell execution times shown in editor
- Focus optimization on slowest cells
- Consider caching for repeated computations
Related Configuration
- Configuration Guide - Complete runtime configuration reference
- Reactivity - Understanding reactive execution
- mo.stop() - Conditional cell execution