Python in Your Browser with Pyodide
Pyodide compiles CPython to WebAssembly. Like webR for R, it runs Python entirely in your browser: no server, no virtual environment, nothing to install.
NumPy, pandas, matplotlib, and scipy are pre-installed. Other packages can be installed with micropip. Variables defined in one cell are available in the next.
Load time is a bit slower than webR (5–10 seconds) because CPython is a larger runtime. Once initialized, subsequent cells are fast.
NumPy
NumPy is available immediately. The vectorization that makes it fast in a normal Python environment carries over to the WASM build:
Try increasing n. The estimate gets more accurate but takes longer. At a million samples you’re typically accurate to 3–4 decimal places.
Matplotlib
Plots render inline. The same Monte Carlo simulation, visualized: points inside the unit circle versus outside.
Pandas
A random walk built as a pandas DataFrame, with a distance-from-origin column:
The central limit theorem in Python
The same CLT demo from the R post: exponential population, sample means converging to normal as n_obs grows. Change the value and re-run:
Installing packages with micropip
Packages not bundled with Pyodide can be installed with micropip. Pure-Python packages generally work; packages with C extensions need a WASM build:
micropip.install is asynchronous, so await is required. It downloads from PyPI and installs into the in-browser environment.
Limitations
A few things don’t work in a WASM environment:
- Threading:
threadingandmultiprocessingare unavailable or limited. - File I/O: no access to your local filesystem. Use
io.StringIO/io.BytesIOfor in-memory file handling. - Network requests:
requestswon’t work. Usepyodide.http.open_urlor JavaScript’sfetchviapyodide.globals. - C-extension packages: packages that rely on compiled C extensions (like
lightgbm,xgboost) only work if a WASM build exists.
For most data science and statistics work the pre-installed stack (NumPy, pandas, matplotlib, scipy, scikit-learn) covers the common cases. Scikit-learn is fully available. That’s a separate post.