Skip to content

App

marimo.App

App(**kwargs: Any)

A marimo notebook.

A marimo notebook is a dataflow graph, with each node computing a Python function.

embed async

embed() -> AppEmbedResult

Embed a notebook into another notebook.

The embed method lets you embed the output of a notebook into another notebook and access the values of its variables.

Running await app.embed() executes the notebook and results an object encapsulating the notebook visual output and its definitions.

Embedded notebook outputs are interactive: when you interact with UI elements in an embedded notebook's output, any cell referring to the app object other than the one that imported it is marked for execution, and its internal state is automatically updated. This lets you use notebooks as building blocks or components to create higher-level notebooks.

Multiple levels of nesting are supported: it's possible to embed a notebook that in turn embeds another notebook, and marimo will do the right thing.

Example
from my_notebook import app
# execute the notebook; app.embed() can't be called in the cell
# that imported it!
result = await app.embed()
# view the notebook's visual output
result.output
# access the notebook's defined variables
result.defs

To embed independent copies of same app object, first clone the app with app.clone():

```python
from my_notebook import app
```

```python
one = app.clone()
r1 = await one.embed()
```

```python
two = app.clone()
r2 = await two.embed()

Returns: An object result with two attributes: result.output (visual output of the notebook) and result.defs (a dictionary mapping variable names defined by the notebook to their values).

run

run(defs: dict[str, Any] | None = None, **kwargs: Any) -> tuple[Sequence[Any], Mapping[str, Any]]

Run the marimo app and return its outputs and definitions.

Use this method to run marimo apps programmatically and retrieve their outputs and definitions. This lets you execute notebooks from other Python scripts. By providing definitions to app.run(), you can override specific cells in the notebook with your own values.

Examples:

Consider a notebook my_notebook.py:

import marimo

app = marimo.App()

with app.setup:
    import pandas as pd


@app.cell
def config():
    batch_size = 32
    learning_rate = 0.01
    return batch_size, learning_rate


@app.cell
def process_data(pd, batch_size, learning_rate):
    data = pd.DataFrame({"x": [1, 2, 3]})
    result = data * batch_size * learning_rate
    return (result,)


if __name__ == "__main__":
    app.run()

To run this app programmatically:

from my_notebook import app

# Run with default values
outputs, defs = app.run()
# defs["batch_size"] == 32, defs["learning_rate"] == 0.01

# Override the specific cell definitions in `config`
outputs, defs = app.run(
    defs={batch_size: 64, learning_rate: 0.001}
)
# defs["batch_size"] == 64, defs["learning_rate"] == 0.001
Definition Override Behavior

When you provide definitions to app.run(), you are completely overriding the definitions of cells that define those variables:

  • The cells that originally defined those variables will not execute
  • You must provide all the definitions that a cell would normally produce
  • Cells that depend on the overridden variables will use your provided values
PARAMETER DESCRIPTION
defs

You may pass values for any variable definitions as keyword arguments. marimo will use these values instead of executing the cells that would normally define them. Cells that depend on these variables will use your provided values.

TYPE: dict[str, Any] DEFAULT: None

**kwargs

For forward-compatibility with future arguments.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Sequence[Any]

A tuple containing:

Mapping[str, Any]
  • Sequence of cell outputs (visual outputs from each cell)
tuple[Sequence[Any], Mapping[str, Any]]
  • Mapping of variable names to their values (definitions)
Environment Variables

AppMeta

marimo.app_meta

app_meta() -> AppMeta

Get the metadata of a marimo app.

The AppMeta class provides access to runtime metadata about a marimo app, such as its display theme and execution mode.

Examples:

Get the current theme and conditionally set a plotting library's theme:

import altair as alt

# Enable dark theme for Altair when marimo is in dark mode
alt.themes.enable(
    "dark" if mo.app_meta().theme == "dark" else "default"
)

Show content only in edit mode:

# Only show this content when editing the notebook
mo.md("# Developer Notes") if mo.app_meta().mode == "edit" else None

Get the current request headers or user info:

request = mo.app_meta().request
print(request.headers)
print(request.user)
RETURNS DESCRIPTION
AppMeta

An AppMeta object containing the app's metadata.

TYPE: AppMeta