Python and WebAssembly

Run Python in the Browser

The Python editor has a Browser Python mode: a real CPython interpreter, compiled to WebAssembly, runs your code on your own device instead of on a server, with NumPy, pandas and SciPy included. There is nothing to install.

How to run Python in the browser

  1. Open the Python editor.
  2. Under the Run button, select Switch and choose Browser Python instead of Execution Server.
  3. Select Run. The first time, the browser downloads Python and the data packages (about 35 MB in total) and caches them, so later runs start much faster.
  4. Import NumPy, pandas, SciPy or scikit-learn as usual. The example below prints emscripten as the platform when it runs in the browser, and linux when it runs on the server.
browser.py
browser.py
import sys
import numpy as np
import pandas as pd

print("Python", sys.version.split()[0], "on", sys.platform)

df = pd.DataFrame({"city": ["Austin", "Boston", "Chicago"], "temp_f": [91, 77, 84]})
df["temp_c"] = ((df["temp_f"] - 32) * 5 / 9).round(1)
print(df)
print("Mean (C):", np.mean(df["temp_c"]).round(1))

Browser Python or Execution Server

Browser PythonExecution Server (default)
Where the code runsOn your device, in a Web WorkerOn our execution server
Keyboard inputA box asks for the input lines before the runinput() asks while the program runs
PackagesNumPy, pandas, SciPy, Matplotlib, scikit-learn, openpyxlA larger installed set, including seaborn, requests and more
Internet access from your codeNo sockets, so requests does not workYes
SpeedA large first download, then a fast start; heavy loops are slower than native PythonNative speed, a short connection time per run
DebuggerNo: switch to Execution Server to debugYes: breakpoints, stepping, variables and the call stack

How Python runs in a web page

Browsers run JavaScript and WebAssembly, not Python. The Pyodide project compiles CPython and scientific packages to WebAssembly, so the browser can run the same interpreter you would install on a computer. Other projects take different routes: PyScript runs Python inside HTML pages on top of Pyodide or MicroPython, JupyterLite runs notebooks in the browser, and Brython translates Python to JavaScript.

To run Python in your own page, load Pyodide and call runPython:

index.html
<script src="https://cdn.jsdelivr.net/pyodide/v0.25.1/full/pyodide.js"></script>
<script>
  async function main() {
    const pyodide = await loadPyodide();
    console.log(pyodide.runPython("sum(i * i for i in range(10))"));  // 285
  }
  main();
</script>

To put a runnable example on a blog or in course notes without writing any code, use the Run on CodeUtility button.

Limits of Python in the browser

  • No direct network socketsBrowser security does not allow raw sockets, so libraries such as requests do not work as they are. Use the Execution Server for programs that call web APIs.
  • No desktop windowsTkinter and other GUI toolkits cannot open windows. For charts, use the Matplotlib editor, which shows the figure next to the code; turtle drawings work on the Python turtle page.
  • MemoryA browser tab has less memory than a computer, so very large datasets are better on the Execution Server.
  • First loadPython and its packages are downloaded once, about 35 MB; on a slow connection the first run takes a while.

Frequently asked questions

How can Python run in a browser?

CPython is compiled to WebAssembly by the Pyodide project, so the browser runs a real Python interpreter. The editor downloads it once, then your code runs on your own device.

Where does my code run in Browser Python mode?

On your own device: the browser executes the program, not our server. The editor still records each run, including the code, for run history and usage statistics, as it does for runs on the Execution Server.

Which packages are available?

NumPy, pandas, SciPy, Matplotlib, scikit-learn and openpyxl load in Browser Python mode. The Execution Server has a larger set of installed packages, including seaborn and requests.

Why is the first run slower?

The browser first downloads the Python runtime and the data packages (NumPy, pandas, SciPy, Matplotlib and scikit-learn), about 35 MB in total. They are cached, so later runs start much faster.

Can I use input() in the browser?

In Browser Python mode, type the input lines before you run the program. For a program that asks questions one at a time, use the Execution Server, which is the default.