Live demos
<PyodideDemo> runs the project’s Python in the reader’s browser with
Pyodide (Python compiled to WebAssembly): nothing is installed and nothing
is sent to a server. Move the sliders to step the oscillator again:
Loading Python (about 10 s the first time)…
The Python of this demo
"""The live demo of the example site: a harmonic oscillator, stepped with example_pkg's leapfrog.
It runs in the browser (Pyodide); `run` takes the values of the sliders and returns the figure."""
import numpy as np
from example_pkg import Leapfrog
def run(dt=0.3, periods=5): """Step x'' = -x from x = 1, v = 0 and compare the positions with cos(t).""" steps = int(round(2 * np.pi * periods / dt)) positions = Leapfrog().run(1.0, 0.0, dt, steps, lambda x: -x) t = dt * np.arange(steps + 1) fine = np.linspace(0, t[-1], 400) error = np.abs(positions - np.cos(t)).max() return { "figure": { "data": [ {"x": fine, "y": np.cos(fine), "mode": "lines", "name": "exact"}, {"x": t, "y": positions, "mode": "markers", "name": "leapfrog"}, ], "layout": {"xaxis": {"title": {"text": "time"}}, "yaxis": {"title": {"text": "position"}}}, }, "numbers": {"Steps": steps, "Largest error": float(error)}, }The Python
Section titled “The Python”The demo is a Python file next to the page, src/components/<name>_demo.py, with a function
run that takes one keyword argument per control and returns a dict:
figure: a Plotly figure as a dict (data,layout), or a list of them, one per caption (captions={[...]}); NumPy arrays are fine;numbers(optional):{label: value}, shown below the figures;text(optional): a sentence below them.
Give every argument of run a default, the value its control starts at: the Pyodide check runs
run() with them. Keep a run short, a fraction of a second: it blocks the page while it computes.
The page
Section titled “The page”import PyodideDemo from '@max-models/starlight-theme/components/PyodideDemo.astro';import code from '../../../components/leapfrog_demo.py?raw';
<PyodideDemo code={code} controls={[ { name: 'dt', label: 'Time step', min: 0.05, max: 1.5, step: 0.05, value: 0.3 }, { name: 'density', label: 'Density', min: 16, max: 21, value: 1e19, log: true, unit: 'm<sup>-3</sup>' }, { name: 'gas', label: 'Gas', options: ['Ar', 'He'], value: 'Ar' }, ]} caption="What the figure shows."/>A log slider moves the exponent: min and max are powers of ten, value the value itself.
Further props: packages (more packages for micropip, e.g. ['plotly']), aspect (width over
height of the figures), showCode (the folded Python below the demo, default on).
The wheels
Section titled “The wheels”The page installs the project’s wheel, and on the dev site the wheels of its devel dependencies
([tool.uv.sources]), from pyodide/wheels.json of the site. npx starlight-pyodide wheels in
docs/ builds them into public/pyodide/ (add that to .gitignore); in CI the docs workflow
does it with pyodide: true. The other dependencies come from Pyodide’s own packages (NumPy,
SciPy, …) or from PyPI, and must be pure Python.
The check
Section titled “The check”The reusable workflow pyodide.yml checks that the package runs in Pyodide: it installs the
wheels in Pyodide under Node, imports every module of the package, runs each demo’s run() and,
optionally, a test directory (pytest: tests/portable). Locally, in docs/:
npx starlight-pyodide wheelsnpm install --no-save pyodide@314.0.6npx starlight-pyodide check --pytest ../tests/portable