Skip to main content

A marimo notebook: exploring parameters

An interactive marimo notebook with sliders for n_neighbors and min_dist. It runs right here in your browser, or locally with live computation.

marimo notebooks are reactive Python notebooks stored as plain .py files. The notebook below runs in your browser: move a slider and the plot updates.

Open the notebook in a new tab (it takes a few seconds to load Python in your browser).

At its core, the notebook is two sliders and one polars-umap call:

n_neighbors = mo.ui.slider(steps=[5, 15, 50, 200], value=15, label="n_neighbors")
min_dist = mo.ui.slider(steps=[0.0, 0.1, 0.5, 0.99], value=0.1, label="min_dist")

digits.umap.fit_transform(
    "pixels", n_neighbors=n_neighbors.value, min_dist=min_dist.value, random_state=42
)
NotePrecomputed in the browser, live on your machine

The browser runs Python through WebAssembly (Pyodide). polars-umap is a compiled Rust extension without a WebAssembly build, so in the browser the notebook shows a grid of embeddings that polars-umap computed beforehand, with scripts/precompute_notebook_data.py. On your own machine, it runs polars-umap live.

Run it locally

The notebook declares its dependencies inline (PEP 723), so marimo can install them in an isolated environment:

uvx marimo edit --sandbox \
  https://raw.githubusercontent.com/jeroenjanssens/polars-umap/main/notebooks/explore-umap.py

Or, in a project that already has polars-umap:

uv add marimo altair scikit-learn
uv run marimo edit explore-umap.py

The source is in notebooks/explore-umap.py.

Using polars-umap in your own notebook

polars-umap needs nothing special in marimo. A UI element feeding its parameters makes the embedding recompute whenever the element changes:

import marimo as mo
import polars as pl
import polars_umap  # noqa: F401

n_neighbors = mo.ui.slider(2, 100, value=15, label="n_neighbors")
n_neighbors
df.umap.fit_transform("embedding", n_neighbors=n_neighbors.value, random_state=0)

UMAP on a few thousand rows takes a fraction of a second, which keeps the notebook responsive. For larger data, use mo.ui.slider(..., debounce=True) so the embedding only recomputes when the slider is released.