November 7, 2025

marimo
next-gen notebooks

Model Risk Seminar

Rubén Hernández Murillo

Literate Programming

  • Notebooks are part of the literate programming paradigm
    • Software as literature
      • Documentation with embedded code
    • Introduced by Donald Knuth in 1984

First notebooks

  • WEB
    • Pascal
    • TeX

Notebooks are great

  • Notebooks provide interactive environment
    • users can mix
      • text
      • code
      • graphics
  • Great for generating reports
  • Risky to use in production

Production environment

  • Live, operational space where you deploy the
    latest versions of software for the intended end-users
    • final stage of development lifecycle
    • after passing through testing
    • optimized for stability, reliability, and security
  • Ideally
    • code should run non-interactively
    • end-users should not be able to modify it

Jupyter notebooks

  • Convenient web-based interactive environment:
    • support many languages (Python, R, Julia, etc.)
  • Great for
    • prototyping code
    • showcasing data analysis
  • Not production-friendly1

marimo notebooks

  • Convenient web-based interactive environment
    • Python-first (also SQL)
  • Great for
    • prototyping code
    • showcasing data analysis
  • Production-friendly

Jupyter notebooks have a reproducibility problem

  • Large-scale study of 1,159,166 unique Jupyter notebooks collected from GitHub (Pimentel et al. 2019, IEEE)
  • Out of 863,878 valid notebooks (i.e., with defined Python version and execution order)
    • only 24.11% executed without errors
    • only 4.03% produced the same results

Root causes of the reproducibility problem

  • hidden state
  • lack of dependencies management
  • challenging version control

marimo notebooks are reproducible by design

  • no hidden state
  • built-in dependencies management
  • version control is a breeze

Hidden state

  • Running cells in sequential order returns different outputs
    compared with the existing outputs
  • In Jupyter notebooks
    • cells can run out-of-order
    • variables can be redefined or deleted
      • dependent variables are not updated
    • ⇒ unexpected results

Reactive execution

  • In marimo
    • cells run-order is determined by DAG
    • variables cannot be redefined or deleted
    • dependent variables are updated reactively1
    • ⇒ no ambiguity

Jupyter and dependencies management

  • Not built-in
    • requires additional steps:
  • create virtual environment

    python -m venv .venv
  • install dependencies with

    pip install pandas numpy ...
  • track Python version with pyproject.toml file

    [project]
    requires-python = ">=3.10"
  • track packages versions with requirements.txt file

    pip freeze > requirements.txt

marimo and dependencies management

  • built-in
    • can manage dependencies with uv1 or pip
    • dependencies are installed on-the-fly

marimo and uv

  • marimo uses PEP 723 and uv for inlining dependencies

  • Inlining dependencies:
    1. ex-ante

      marimo edit --sandbox example_notebook.py
    2. ex-post

      uv add --script example_notebook.py 'altair'
  • Using inline dependencies:

    1. edit with uvx

      uvx marimo edit --sandbox example_notebook.py
    2. run sandboxed notebooks as scripts

      uv run example_notebook.py

Jupyter and version control

  • Not git-friendly (out-of-the-box)1
  • Jupyter notebooks are (very busy) JSON files
    • small code changes ⇒ big git diff changes
  • metadata noise:
-    "execution_count": 42,
+    "execution_count": 43,

marimo and version control

  • git-friendly (by default)
  • marimo notebooks are Python scripts
    • small code changes ⇒ small git diff changes
  • meaningful changes:
-def predict(x):
-    return model(x)
+def predict(x):
+    return model(x).clip(0, 1)

  • Jupyter is JSON, marimo is Python

Jupyter in production

  • Jupyter notebooks may run non-interactively
    • requires additional steps:
  • converting to Python scripts
jupyter nbconvert --to script Untitled12.ipynb
  • using papermill
import papermill
pm.execute_notebook(
  'path/to/input.ipynb',
  'path/to/output.ipynb',
  parameters=dict(alpha=0.6, ratio=0.1)
)

marimo in production

  • marimo notebooks run directly as Python scripts

    python notebook.py
    • additional deployment options:
  • run as interactive web app:

    marimo run notebook.py
  • run in the browser (without local Python):

    marimo export html-wasm notebook.py -o notebook.wasm.html

Conclusion

marimo

  • Provides new approach for Python notebooks
  • Advantages over Jupyter
    • no hidden state
    • built-in dependencies management
    • version control ease
  • ⇒ Production friendly

Next steps

  • Install uv
pip install uv
  • Try marimo
uvx marimo tutorial intro  # try without installing
  • Install it
uv tool install marimo   # as global tool
uv add marimo            # add to environment
  • Migrate old notebooks
marimo convert old_jupyter.ipynb -o modern_marimo.py

example_notebook.py

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "altair==5.5.0",
#     "duckdb==1.3.2",
#     "marimo",
#     "matplotlib==3.10.3",
#     "numpy==2.3.2",
#     "pandas==2.3.3",
#     "polars==1.31.0",
#     "pyarrow==21.0.0",
#     "sqlglot==27.3.1",
# ]
# ///

import marimo

__generated_with = "0.17.7"
app = marimo.App(width="medium", css_file="", sql_output="native")


@app.cell
def _():
    import marimo as mo
    import polars as pl
    import json
    import altair as alt
    return alt, mo, pl


@app.cell
def _(mo):
    mo.md(r"""
    ## Example: No hidden-state
    """)
    return


@app.cell
def _():
    x = 333
    x
    return (x,)


@app.cell
def _(x):
    y = x + 15
    y
    return (y,)


@app.cell
def _(x, y):
    z = x + y
    z
    return


@app.cell
def _(mo):
    mo.md(r"""
    ## Example: No redefining variables
    """)
    return


@app.cell
def _():
    # Try to redefine x
    # x = 7
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""
    ## Example: Reactive plot
    """)
    return


@app.cell
def _(mo):
    mo.md(r"""
    ### Load data
    """)
    return


@app.cell
def _(pl):
    url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
    names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class']
    dataset = pl.read_csv(url, has_header=False, new_columns=names)
    return (dataset,)


@app.cell
def _(dataset):
    # Inspect data

    dataset
    return


@app.cell
def _(mo):
    mo.md(r"""
    ### Iris dataset scatter-plot
    """)
    return


@app.cell
def _():
    x_var = "petal_width"
    return (x_var,)


@app.cell
def _():
    y_var = "sepal_width"
    return (y_var,)


@app.cell
def _(mo, x_var, y_var):
    mo.md(rf"""
    ## Chart with <span class="highlight-red">x_var</span>={x_var} and <span class="highlight-red">y_var</span>={y_var}.
    """)
    return


@app.cell
def _(alt, dataset, x_var, y_var):
    # Chart with parameterized x_var and y_var:

    (
        alt.Chart(dataset).mark_point(tooltip=True).encode(
            x=x_var,
            y=y_var,
            color="class",
        )
        .properties(width=500)
        .configure_scale(zero=False)
    )
    return


@app.cell
def _(mo):
    mo.md(r"""
    ## Example: Interactive widgets
    """)
    return


@app.cell
def _():
    import math
    import numpy as np
    import matplotlib.pyplot as plt
    return np, plt


@app.cell(hide_code=True)
def _(mo):
    b = mo.ui.slider(start=0.1, step=0.01, stop= 1, label="b")
    a = mo.ui.slider(start=1, step=0.50, stop= 3, label="a")
    return a, b


@app.cell
def _(a, b, mo):
    mo.hstack([b, mo.md("b coefficient"), a, mo.md("a exponent") ])
    return


@app.cell(hide_code=True)
def _(a, b, mo):
    mo.md(rf"""
    Let's plot the function: $f(x)=b x^{{a}} = {b.value:0.1f} x^{{{a.value:0.1f}}}$ over the interval $[0, 10]$.
    """)
    return


@app.cell(hide_code=True)
def _(a, b, np, plt):
    v = np.linspace(0, 10)
    plt.plot(v, b.value*v**(a.value))
    plt.gca()
    return


@app.cell
def _(mo):
    mo.md(r"""
    ## Example: Using `SQL`
    """)
    return


@app.cell
def _(dataset, mo):
    df_sql = mo.sql(
        f"""
        SELECT
            class,
            MEAN(sepal_length) AS avg_sepal_len,
            SUM(sepal_width) AS sum_sepal_width
        FROM
            dataset
        GROUP BY
            class
        ORDER BY
            class;
        """
    )
    return (df_sql,)


@app.cell
def _(df_sql):
    df_sql
    return


@app.cell
def _(df_sql):
    # Output of SQL query is DuckDb native
    # Use .pl() method to obtain a polars dataframe
    df_sql.pl()
    return


@app.cell
def _(alt, df_sql):
    # Altair bar chart

    alt.Chart(df_sql.pl()).mark_bar(tooltip=True).encode(
        x='class',
        y='avg_sepal_len' 
    )
    return


@app.cell
def _(alt, df_sql):
    # Shorthand using polars plot method

    df_sql.pl().plot.bar(x=alt.X("class"), y = "avg_sepal_len")
    return


@app.cell
def _(alt, df_sql):
    # Scatter plot

    df_sql.pl().plot.point(x=alt.X("avg_sepal_len"), y = "sum_sepal_width", color = "class")
    return


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

Resources