Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/whitphx/stlite/llms.txt

Use this file to discover all available pages before exploring further.

Making HTTP requests from Python code running in the browser is not straightforward. Standard networking libraries like requests rely on system-level socket calls that do not exist in a WebAssembly sandbox. Stlite works around this by automatically patching popular libraries via pyodide-http and by exposing Pyodide’s own async HTTP primitives — so you can make network calls without changing how you normally write Python code.

Supported libraries

LibraryHow it worksNotes
requestsPatched by pyodide-httpLimited to supported classes/methods
urllibPatched by pyodide-httpSame limitation as requests
urllib3 ≥ 2.2.0Native Pyodide emscripten backendNo patch needed
pyodide.http.pyfetch()Native Pyodide Fetch API wrapperAsync — use with await
pyodide.http.open_url()Native Pyodide synchronous fetchSynchronous — blocks the event loop
Stlite automatically enables the pyodide-http patches at startup, so requests and urllib work out of the box for the methods and classes listed in the pyodide-http supported packages list.
Standard networking libraries do not work in Pyodide by default. Stlite applies the pyodide-http patch automatically so you don’t have to, but the patch only covers a subset of the requests and urllib APIs.

Using requests (synchronous)

Because Stlite applies the pyodide-http patch automatically, you can use requests with the familiar synchronous API — no extra setup required. Simply include requests in your requirements if it isn’t already available:
import streamlit as st
import requests

st.title("Fetch data with requests")

url = "https://jsonplaceholder.typicode.com/todos/1"
response = requests.get(url)

if response.ok:
    data = response.json()
    st.json(data)
else:
    st.error(f"Request failed: {response.status_code}")
Not all requests methods and classes are supported through the pyodide-http patch. Check the supported packages list if you encounter unexpected errors.

Using pyodide.http.pyfetch() (async)

pyodide.http.pyfetch() is Pyodide’s native Python wrapper around the browser’s Fetch API. It is async, which means you must await it. In Stlite, top-level await is supported, so you can use it directly in your script without wrapping it in asyncio.run().
import pyodide.http
import streamlit as st

url = "https://jsonplaceholder.typicode.com/posts/1"

response = await pyodide.http.pyfetch(url)
data = await response.json()

st.title(data["title"])
st.write(data["body"])
The response object supports several awaitable methods:
# Read response as raw bytes
raw_bytes = await response.bytes()

# Read response as text
text = await response.string()

# Read response as parsed JSON
parsed = await response.json()
Top-level await is a key feature of Stlite. See the Top-Level Await guide to understand why asyncio.run() won’t work and how to structure async code in your Streamlit scripts.

Using pyodide.http.open_url() (synchronous)

pyodide.http.open_url() is a synchronous alternative provided by Pyodide. It returns a io.StringIO-like object you can read directly:
import pyodide.http
import streamlit as st

url = "https://example.com/data.txt"
response = pyodide.http.open_url(url)
content = response.read()

st.text(content)
open_url() blocks the event loop while the request is in flight. Using it inside st.spinner() will prevent the spinner from rendering. If you need a spinner, use pyfetch() with await instead, or add await asyncio.sleep(0.1) before the call — see the Top-Level Await guide for the workaround.

Limitations

CORS restrictions

HTTP requests made from inside the browser are subject to the browser’s CORS (Cross-Origin Resource Sharing) policy. Even if a request works in server-side Python, it may be blocked in Stlite if the target server does not include the appropriate Access-Control-Allow-Origin headers. There is no way to bypass CORS from within Stlite — this is enforced by the browser itself. If you hit a CORS error, you need either:
  • A server that sets permissive CORS headers, or
  • A CORS proxy that forwards requests on your behalf.

Partial API support for requests and urllib

The pyodide-http patch does not implement the full requests or urllib API surface. Features like streaming responses, session cookies, advanced authentication, and proxy support may not work. Always consult the pyodide-http supported packages documentation when you encounter unexpected behaviour.

urllib3 version requirement

urllib3 requires version 2.2.0 or later for Pyodide/emscripten compatibility. Earlier versions will not work in Stlite.

Build docs developers (and LLMs) love