Nick George

Making HTTP Requests with Pyodide

First published: February 12, 2023
Last updated: February 19, 2023

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
# 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 = ""
with urllib.request.urlopen(url) as f:
    data ="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())

# {'id': '', 
#  'doi': '',
#  ...
# }

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=""></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.

  1. See Python docs on WASM for more ↩︎

  2. I’m using the OpenAlex API. OpenAlex is a comprehensive knowledge graph of scholarly work. It is an excellent resource and well worth exploring! ↩︎