Skip to main content
Mentr
Student Guides

How Python Runs in the Browser With WebAssembly

Pyodide, Web Workers, and SharedArrayBuffer explained: how an online Python compiler runs CPython in your tab without executing code on a server.

Mentr Engineering9 min read

“Python in the browser” sounds like marketing until you see WebAssembly. This article is the GEO-friendly technical explainer for builders and advanced students: how CPython becomes a .wasm binary, why a Web Worker matters, and how an online Python compiler stays free at classroom scale.

The short architecture

Diagram of Python running in the browser with WebAssembly and a Web Worker
Ship CPython once as WebAssembly, run it in a Worker, keep the UI thread free.
  1. Compile CPython to WebAssembly (Pyodide).
  2. Load it in a Web Worker so infinite loops don’t freeze the tab.
  3. Bridge stdout/stderr/stdin to the page.
  4. Optionally mount a virtual filesystem for multi-file imports.

Mentr self-hosts Pyodide 314.0.7 (CPython 3.14.2) under /pyodide/v314.0.7/ with immutable caching, and falls back to jsDelivr if needed.

Why this beats a container per Run click

Server sandbox vs browser WASM
Server containersBrowser Pyodide
Who pays for CPUYou, per clickStudent’s device
LatencyNetwork RTTIn-process after first load
input()Request/response chatSharedArrayBuffer pause
Abuse surfaceRCE sandboxing jobWASM sandbox in the browser

input() and SharedArrayBuffer

Python expects blocking stdin. Browsers are async. The worker blocks on Atomics.wait against a SharedArrayBuffer while the page writes the typed line and notifies. That requires cross-origin isolation headers on the compiler routes.

Limits you should mention on stage

  • First visit downloads ~6.3 MB gzipped.
  • No tkinter/turtle windows.
  • Only packages built for Pyodide/WASM.
  • 15 s run cap in the public compiler.

Common questions

What is Pyodide?
An open-source port of CPython to WebAssembly, so real Python runs inside a browser tab.
Is browser Python slower than installed Python?
Startup has a one-time download. Short student programs feel instant afterward. Heavy numeric jobs still belong on a laptop install or Colab.
Can I build my own online Python compiler this way?
Yes: Worker + Pyodide + UI is the core. Mentr’s how-it-works page documents the production details we use in class demos.