Marimo / Reactive Python

marimo is an open-source reactive Python notebook framework. It is an opioniated alternative to Jupyter notebooks that offer several improvements over regular .ipynb-notebooks.

The purposes of writing teaching material marimo offers a design that lets one write reactive webpages using pure Python - and is an alternative to both Shiny and ObservableJS.

Quarto integration comes from the quarto-marimo-extension, see GitHub

Examples

  • Basic reactive Python: A basic example showing interactivity with marimo.
  • Numerical integration: An example showing numerical integration. This uses more features of marimo and has much more underlying Python code.
  • Data exploration: A simple data-exploration widget using marimo and altair on the Palmer’s penguins dataset.
  • Chemical reactions: Using sympy to setup up algebraic reaction equations that are then solved numerically.

Technical details

First, the Quarto extension needs to be installed, from the Quarto root

quarto install extension marimo-team/quarto-marimo

This will add the extension to the _extensions-directory which should be comitted to version control.

By default marimo will create an isolated virtual environment per document with packages controllable by the yaml header, for authoring I have found it more convenient to run in the pixi mananged environment, this is enabled with the external-env-setting in the yaml header.

---
title: ...
external-env: true
---

This requires marimo to be installed in that environment, which can be achieved using pixi add marimo.

Then marimo cells can be written using {python .marimo}-cells, for example

```{python .marimo}
import marimo as mo
slider = mo.ui.slider(start=0, stop=5, step=1, label="A slider")
slider
```

Which produces a slider like below

This slider can be referenced in other cells

```{python .marimo}
mo.callout(mo.md(f"The value of the slider is: {slider.value}"),kind="success")
```

This one creates a callout box with text that depends on slider.value.

See marimo docs and quarto-marimo docs for more information.

GitHub pages

When running any packages that are installed in active virtual environment (if external-env: true) or declared in the yaml header (pyproject-key) will be available.

However, if the project is intended for publishing to GitHub pages etc you need to ensure that the packages are Pyodide compatible. The examples above have used

  • NumPy
  • Pandas
  • Matplotlib
  • Altair

See Packages built in Pyodide for more information.