Pyodide is a port of the CPython interpreter compiled to WebAssembly via the Emscripten compiler toolchain. Pyodide allows you to run python code in browser or with the node.js runtime.
In fact, you can navigate to an in browser python terminal and start using python right now, no installation required!
# from the pyodide console!
import sys
print(sys.platform)
# emscripten
Thanks to Emscripten, much of the familiar CPython API is immediately available to use. However, Pyodide runs in the browser sandbox, so certain key functions will be limited, including: “…processes, threading, networking, signals, or other forms of inter-process communication (IPC), is either not available or may not work as on other Unix-like systems. File I/O, file system, and Unix permission-related functions are restricted, too.”1
One missing feature is the ability to do standard HTTP requests2:
# sys.platform == 'emscipten'
import urllib.request
url = "https://api.openalex.org/works/W2741809807"
with urllib.request.urlopen(url) as f:
data = f.read().decode("utf-8")
# Traceback...
# urllib.error.URLError: <urlopen error unknown url type: https>
The Pyodide JavaScript API
Since it runs in the browser, Pyodide can access the JavaScript API, and the Pyodide developers provide an (unstable) pyodide module to access some JavaScript functionality from Python (or vice versa).
To make a standard HTTP request from Pyodide, you can use the pyodide.http module like so:
import pyodide.http
import json
with pyodide.http.open_url(url) as f:
as_json = json.loads(f.getvalue())
as_json
# {'id': 'https://openalex.org/W2741809807',
# 'doi': 'https://doi.org/10.7717/peerj.4375',
# ...
# }
Python as a JavaScript library
Pyodide allows you to run python from the browser (or node.js) just like it was a JavaScript library by simply including the Pyodide bootstrap script in your HTML:
<script src="https://cdn.jsdelivr.net/pyodide/v0.16.1/full/pyodide.js"></script>
While not “full Python”, there are some great examples on the Pyodide website for how you can use Python in your JavaScript apps today.