Skip to main content

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:

Runtime Configuration Options

From marimo/_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

Click the run button on specific stale cells when you need their results:
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 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:
Large outputs are automatically truncated with a download link.

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
Use "strict" mode when working with mutable objects that could create unexpected side effects between cells.

Working with Module Reloading

When developing modules imported by your notebook:
Behavior:
  • "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

ETL notebooks benefit from manual control over execution flow:
Add runtime controls for conditional execution:
Use marimo’s cell timing to identify bottlenecks:
  • Cell execution times shown in editor
  • Focus optimization on slowest cells
  • Consider caching for repeated computations