Marimo 101

Reactive Python notebooks, with SQL on OMOP as the example

Piyush Chaudhari

2026-10-22

What is marimo?

A reactive Python notebook

  • Change a cell, and every cell that depends on it reruns automatically
  • Notebooks are stored as plain .py files
  • Built-in UI elements, SQL cells and interactive charts
  • The same file runs as a notebook, an app, a script or slides
  • Runs locally, on molab, or in the browser through WebAssembly

Every widget in this deck is a live marimo cell running in your browser

Your first reactive cells

In marimo

The second cell reads x.value, so moving the slider reruns it. There are no callbacks and no Run button

How reactivity works

marimo reads each cell to find the names it defines and the names it reads, and builds a dependency graph

  • A cell reruns when anything it reads changes
  • Each global name is defined in one cell
  • Delete a cell → its variables are deleted too
  • _name variables are private to their cell
  • Cells run in dependency order, not page order

flowchart TD
    data["<b>data cell</b><br/>con, run()"]
    ui["<b>UI cell</b><br/>yob = mo.ui.range_slider()"]
    sql["<b>query cell</b><br/>reads yob.value, run()"]
    chart["<b>chart cell</b><br/>reads the query result"]
    data --> sql
    ui --> sql
    sql --> chart
    style ui fill:#FA4616,color:#fff,stroke:#FA4616
    style sql fill:#0021A5,color:#fff,stroke:#0021A5
    style chart fill:#0021A5,color:#fff,stroke:#0021A5

A notebook is a Python file

import marimo

app = marimo.App()


@app.cell
def _(mo):
    yob = mo.ui.range_slider(1935, 2005, value=[1950, 1975])
    yob
    return (yob,)


@app.cell
def _(con, mo, yob):
    where_df = mo.sql(f"""
        SELECT person_id FROM person
        WHERE year_of_birth BETWEEN {yob.value[0]} AND {yob.value[1]}
    """, engine=con)
    return (where_df,)
  • Each cell is a function: its arguments are what it reads, and its return is what it defines
  • marimo writes this for you; you just edit cells
  • Git diffs are clean: there are no outputs or execution counts in the file
  • python notebook.py runs it as a script
  • import it like a module, or test it with pytest

The companion file sql-basics-marimo.py is this whole lesson in that format

The running example: OMOP

To show marimo off, we need real-looking data. We’ll use a synthetic OMOP CDM extract in DuckDB

  • Every OMOP site uses the same tables and columns
  • Clinical facts live in event tables; their names live in concept
  • Concept IDs are real (SNOMED, RxNorm, LOINC); people, dates and values are simulated

erDiagram
    PERSON ||--o{ VISIT_OCCURRENCE : has
    PERSON ||--o{ CONDITION_OCCURRENCE : has
    PERSON ||--o{ DRUG_EXPOSURE : has
    PERSON ||--o{ MEASUREMENT : has
    CONCEPT ||--o{ CONDITION_OCCURRENCE : "condition_concept_id"
    CONCEPT ||--o{ DRUG_EXPOSURE : "drug_concept_id"
    CONCEPT ||--o{ MEASUREMENT : "measurement_concept_id"
    PERSON {
        int person_id PK
        int gender_concept_id FK
        int year_of_birth
    }
    CONDITION_OCCURRENCE {
        int person_id FK
        int condition_concept_id FK
        date condition_start_date
    }
    CONCEPT {
        int concept_id PK
        string concept_name
        string domain_id
    }

In marimo

One hidden cell builds con, run() and the colour palette. Every other cell in the deck depends on it

SQL cells: mo.sql

SQL is a first-class cell type in marimo

  • Write plain SQL; marimo runs it on the engine you choose (here, the DuckDB connection con)
  • The result is a DataFrame you can use in Python
  • {python_expressions} inside the query make it reactive
  • Without an engine, SQL can query Python DataFrames by name
# what a SQL cell looks like in the .py file
person_preview = mo.sql(f"""
    SELECT person_id, year_of_birth
    FROM person
    LIMIT 6
""", engine=con)

SQL: FROM names the table, SELECT picks columns, LIMIT keeps the preview small

UI elements and .value

In marimo

Assign a mo.ui element to a global, display it, and read .value in another cell. That cell now reruns whenever the widget changes

SQL: WHERE keeps matching rows; BETWEEN is inclusive on both ends

A tour of UI elements

Also: text, text_area, checkbox, multiselect, date, file, button, code_editor, chat and more

SQL: ORDER BY sorts; without it, LIMIT returns an arbitrary sample

Layout: stacks, tabs and stats

mo.hstack, mo.vstack, mo.ui.tabs, mo.accordion, mo.callout, mo.stat and mo.sidebar arrange any outputs, including other widgets

SQL: GROUP BY collapses rows; COUNT(*) counts records, COUNT(DISTINCT person_id) counts patients

UI that depends on UI

In marimo

The slider is created from lab_pick.value, so choosing a new lab rebuilds it with a new range and default. In Jupyter this takes observe callbacks

SQL: CASE WHEN derives a column row by row, which is how most lab-based phenotype flags are built

Interactive charts

Click a bar (shift-click for several). The selection is sent back to Python

age_chart = mo.ui.altair_chart(chart, chart_selection="point")
age_chart.value   # a DataFrame of the selected rows

Interactive tables

Click a condition. The selected row drives the query on the right

cond_table = mo.ui.table(df, selection="single")
cond_table.value   # the selected row(s), as a DataFrame

SQL: JOIN links condition_occurrence → drug_exposure → concept on person_id and *_concept_id

Rich outputs: any HTML is reactive

In marimo

This diagram is an SVG string built in Python and shown with mo.Html. It redraws whenever join_kind changes

Forms: batch inputs, run once

Try %disease, _etformin, %blood%

In marimo

.batch() bundles several widgets into one value (a dict). .form() holds that value back until you submit, and mo.stop() halts the cell until there is something to show

Putting it together: a small cohort app

Of patients with type 2 diabetes, how many started metformin within N days of diagnosis?

SQL: each WITH block (a CTE) is a named step, read top to bottom

Your turn: run_button and mo.stop

Write any query against person, visit_occurrence, condition_occurrence, drug_exposure, measurement or concept, then run it

Editing the code doesn’t rerun anything. run_btn.value is True only right after a click, so mo.stop keeps expensive work behind the button

Downloads: mo.download

In marimo

mo.download turns any bytes into a download button. Here the file is rebuilt every time the year changes

Pipe-delimited with every field quoted survives commas inside concept names. The year leads the filename so extracts sort by period

Beyond these slides

One .py file: notebook, app, script, slides or web page

Running marimo

# editable notebook, isolated env from the file header
uvx marimo edit --sandbox sql-basics-marimo.py

# the same file as an app (code hidden)
uvx marimo run --sandbox sql-basics-marimo.py

# or as a plain script
uv run sql-basics-marimo.py

# a static web page that runs Python in the browser
uvx marimo export html-wasm sql-basics-marimo.py -o docs

# bring an existing Jupyter notebook over
uvx marimo convert notebook.ipynb -o notebook.py

Only in the native editor

  • SQL cells with a schema browser for con
  • A dependency explorer that draws the cell graph
  • mo.ui.dataframe and mo.ui.data_explorer for no-code exploration
  • Vertical, grid and slides layouts for the same notebook
  • molab: open any notebook on GitHub in the cloud

How this deck works

engine: marimo in the front matter turns each ```{python .marimo} block into a marimo cell. Quarto draws the slides, and the cells run in your browser through Pyodide

Recap: the marimo toolkit

Idea API Slide
Reactive cells change a cell → its dependents rerun What is marimo?
SQL cells mo.sql(f"...", engine=con) SQL cells
UI elements mo.ui.slider, dropdown, radio, number, switch, … → .value UI elements, tour
Layout mo.hstack, vstack, ui.tabs, accordion, stat, callout Layout
Dependent widgets create a widget from another widget’s .value UI that depends on UI
Interactive charts mo.ui.altair_chart(..., chart_selection="point").value Interactive charts
Interactive tables mo.ui.table(df, selection="single").value Interactive tables
Rich output mo.md(f"..."), mo.Html(svg) Rich outputs
Forms mo.md("{a} {b}").batch(a=..., b=...).form() Forms
Controlled execution mo.ui.run_button(), mo.stop(condition, output) Your turn
Downloads mo.download(data, filename) Downloads

The full SQL lesson, including string functions, subqueries, set operations and window functions, is in sql-basics-marimo.py and sql-basics-jupyter.ipynb

References

These slides are made using the following tools -

  • Marimo - a reactive Python notebook; see also the docs and molab
  • Quarto - a scientific and technical publishing system built on Pandoc, with the quarto-marimo engine
  • DuckDB - an in-process SQL OLAP database management system
  • OMOP CDM - the OHDSI common data model used for the example data