Skip to main content

Key concepts

marimo introduces a new way of thinking about notebooks through reactive execution and dataflow graphs. This guide explains the fundamental concepts that power marimo.

Reactivity

Reactivity is marimo’s core feature: when you run a cell, marimo automatically runs all cells that depend on it.

How reactivity works

marimo analyzes your code to build a dependency graph:
When you change x in Cell 1, marimo automatically re-runs Cell 2. This keeps your notebook state consistent without manual intervention.

References and definitions

marimo tracks two things for each cell:
  • Definitions (defs) - Global variables the cell creates or modifies
  • References (refs) - Global variables the cell reads
You can inspect these:

The dataflow graph

marimo creates a directed acyclic graph (DAG) where:
  • Nodes represent cells
  • Edges represent dependencies between cells
When you run a cell, marimo runs its descendants in topological order.
marimo prevents cycles in the dependency graph. If you try to create circular dependencies, marimo will show an error.

Lazy execution mode

For expensive computations, you can disable automatic execution:
  1. Click the runtime dropdown in the notebook footer
  2. Change from “autorun” to “lazy”
In lazy mode, marimo marks dependent cells as stale instead of running them automatically. You stay in control while maintaining state guarantees.

Cells

A marimo notebook is composed of cells - small blocks of Python code that form the nodes of the dataflow graph.

Cell structure

Cells in marimo are Python functions decorated with @app.cell:
Each cell:
  • Is a function that returns a tuple of variables to make global
  • Takes its dependencies as function parameters
  • Can have a descriptive name or use __ for anonymous cells

No hidden state

Unlike Jupyter, marimo prevents hidden state:
  • Variables only exist if their defining cell has run
  • Delete a cell and its variables are removed from memory
  • Cells cannot be run out of order
If you delete Cell 1, any cell referencing x will error - marimo won’t let you use undefined variables.

Multiple definitions error

You cannot define the same variable in multiple cells:
This prevents ambiguity about which definition is active.
Use different variable names or combine related code into a single cell to avoid multiple definition errors.

Interactive elements

marimo’s UI elements automatically trigger reactivity when their values change.

Creating UI elements

Import from marimo.ui:

Accessing values

Use .value to get the current value:
When a user interacts with a UI element, marimo re-runs all cells that reference it.

Batching UI elements

Combine multiple inputs into a single object:
Access batched values:

Forms

Prevent UI elements from triggering updates until submitted:
The form only updates dependent cells when submitted, not on every keystroke.

Notebooks vs apps vs scripts

marimo notebooks can be used in three modes:

Edit mode (notebooks)

Interactive development environment:
  • Full code editing
  • Interactive outputs
  • Cell execution controls
  • Variable inspector
  • Reactive updates
Use for:
  • Data exploration
  • Research and analysis
  • Prototyping
  • Interactive development

Run mode (apps)

Deploy as a web app:
  • Code is hidden
  • Only UI elements and outputs shown
  • Still fully reactive
  • Professional presentation
Use for:
  • Dashboards
  • Internal tools
  • Sharing with non-technical users
  • Demos and presentations

Script mode

Execute as a Python script:
  • Runs top to bottom in dependency order
  • No browser required
  • Can accept command-line arguments
  • Outputs to terminal
Use for:
  • Automation
  • Batch processing
  • CI/CD pipelines
  • Scheduled jobs
Access CLI arguments:

State management

marimo provides mo.state() for managing mutable state:
State updates trigger reactivity just like variable changes.
Use state sparingly. Prefer direct variable definitions when possible for clearer dataflow.

Package management

marimo automatically manages dependencies:

Auto-install on import

When you import a package that’s not installed:
marimo detects the missing package and offers to install it.

Inline dependencies

Define requirements in the notebook:
marimo can create isolated environments based on these requirements.

Sandbox mode

Run notebooks in isolated environments:
Each notebook gets its own virtual environment with dependencies auto-installed.

Dynamic outputs

Markdown with variables

Create markdown that updates with your data:

Conditional rendering

Show different outputs based on conditions:

Layouts

Organize outputs with flexible layouts:

Working with data

SQL cells

Query dataframes and databases directly:
The result is automatically available as a Python dataframe.

Interactive dataframes

Explore dataframes with built-in interactivity:
Users can page through millions of rows with no code required.

Next steps

Guides

Dive deeper into specific features and workflows

API reference

Explore the complete marimo API documentation