Python in the Browser: Symbolic Computation with Pyodide
Pyodide is a WebAssembly port of CPython. It runs entirely in the browser — there is no server, no backend, no API call. This post shows how to embed a live Python computation environment in a blog post.
Note: Pyodide loads approximately 10 MB on first use. There will be a brief loading period. Subsequent visits use the browser cache.
Live Symbolic Computation (SymPy)
Type a SymPy expression and click Compute. The result is rendered as LaTeX using KaTeX.
Example expressions to try
diff(exp(x)*sin(x), x)limit(sin(x)/x, x, 0)factor(x**3 - x**2 - x + 1)series(cos(x), x, 0, 8)solve(x**2 - 2, x)Matrix([[1,2],[3,4]]).eigenvals()
How It Works
Pyodide loads the full CPython 3.11 interpreter as a WebAssembly binary, then fetches SymPy’s wheel file. All computation happens in the browser — the server only ever delivers static files.
The integration pattern for any post:
<script src="https://cdn.jsdelivr.net/pyodide/v0.25.0/full/pyodide.js"></script>
<script>
async function main() {
const pyodide = await loadPyodide();
await pyodide.loadPackage(["numpy", "sympy"]); // add any pure-Python packages
const result = await pyodide.runPythonAsync(`
import numpy as np
np.linalg.norm([3, 4])
`);
document.getElementById("output").textContent = result; // → 5.0
}
main();
</script>
Available packages include numpy, scipy, sympy, matplotlib (static output via plt.savefig), and most pure-Python packages via pyodide.loadPackage("micropip") → micropip.install("packagename").