Python in the browser
import { Aside } from ‘@astrojs/starlight/components’;
Run real Python inside the page via Pyodide, loaded on
demand. Access via sdk.python. It runs any Python — numpy, pandas,
your own logic — and reaches the outside world only through JS functions you
explicitly inject.
Methods
Section titled “Methods”run<T>(code, options?): Promise<{ result: T; stdout: string; stderr: string }>ready(options?): Promise<void> // preload the runtimereset(): void // drop the cached runtimereadonly loaded: booleaninterface PythonRunOptions { globals?: Record<string, unknown>; // JS values/functions exposed to Python packages?: string[]; // e.g. ['numpy', 'pandas'] install?: string[]; // pure-Python wheels via micropip onStdout?: (line: string) => void; // streamed print() output onStderr?: (line: string) => void;}Compute
Section titled “Compute”const { result } = await sdk.python.run(`import numpy as npfloat(np.mean([1, 2, 3, 4]))`, { packages: ['numpy'] });
console.log(result); // 2.5Inject your own callbacks
Section titled “Inject your own callbacks”Python can await async JS functions you pass in globals:
await sdk.python.run(`for wk in weeks: await run_sql(TEMPLATE.replace("{W}", wk)) log(f"{wk} done")`, { globals: { weeks, TEMPLATE, run_sql: async (s) => fetchData(s), log: console.log },});No manifest needed — sdk.python stores no artifact data. Keep long jobs chunked
and stream progress via onStdout, since heavy compute blocks the main thread.