Debian Codegraph @

docs/reference/contrib/emscripten.rst

raw
1 Pyodide, Emscripten, and PyScript
2 =================================
3
4 From the Pyodide documentation, `Pyodide <https://pyodide.org>`_ is a Python distribution for the browser and Node.js based on WebAssembly and `Emscripten <https://emscripten.org/>`_.
5 This technology also underpins the `PyScript framework <https://pyscript.net/>`_ and `Jupyterlite <https://jupyterlite.readthedocs.io/>`_, so should work in those environments too.
6
7 Starting in version 2.2.0 urllib3 supports being used in a Pyodide runtime utilizing
8 the `JavaScript fetch API <https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API>`_
9 or falling back on `XMLHttpRequest <https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest>`_
10 if the fetch API isn't available (such as when cross-origin isolation
11 isn't active). This means you can use Python libraries to make HTTP requests from your browser!
12
13 Because urllib3's Emscripten support is API-compatible, this means that
14 libraries that depend on urllib3 may now be usable from Emscripten and Pyodide environments, too.
15
16 .. warning::
17
18 **Support for Emscripten and Pyodide is experimental**. Report all bugs to the `urllib3 issue tracker <https://github.com/urllib3/urllib3/issues>`_.
19 Currently Node.js support is very experimental - see the description below.
20
21 It's recommended to `run Pyodide in a Web Worker <https://pyodide.org/en/stable/usage/webworker.html#using-from-webworker>`_
22 in order to take full advantage of features like the fetch API which enables streaming of HTTP response bodies.
23
24 Getting started
25 ---------------
26
27 Using urllib3 with Pyodide means you need to `get started with Pyodide first <https://pyodide.org/en/stable/usage/quickstart.html>`_.
28 The Pyodide project provides a `useful online REPL <https://pyodide.org/en/stable/console.html>`_ to try in your browser without
29 any setup or installation to test out the code examples below.
30
31 One minor note - when running Pyodide code from JavaScript, if you use ``pyodide.runPythonAsync`` rather
32 than ``pyodide.runPython``, urllib3 can sometimes run more efficiently. It is generally always worth using
33 ``runPythonAsync``.
34
35 urllib3's Emscripten support is automatically enabled if ``sys.platform`` is ``"emscripten"``, so no setup is required beyond installation and importing the module.
36
37 urllib3 is packaged with the default Pyodide build, so you should be able to use it as normal.
38
39 .. code-block:: python
40
41 import urllib3
42 resp = urllib3.request("GET", "https://httpbin.org/anything")
43
44 print(resp.status) # 200
45 print(resp.headers) # HTTPHeaderDict(...)
46 print(resp.json()) # {"headers": {"Accept": "*/*", ...}, ...}
47
48 Because `Requests <https://requests.readthedocs.io/en/latest/>`_ is built on urllib3, Requests also works out of the box:
49
50 .. code-block:: python
51
52 import requests
53 resp = requests.request("GET", "https://httpbin.org/anything")
54
55 print(resp.status_code) # 200
56 print(resp.headers)
57
58 Features
59 --------
60
61 Because we use JavaScript APIs under the hood, it's not possible to use all of urllib3 features.
62 Features which are usable with Emscripten support are:
63
64 * Requests over HTTP and HTTPS
65 * Timeouts
66 * Retries
67 * Streaming (with Web Workers and Cross-Origin Isolation)
68 * Redirects (urllib3 controls redirects in Node.js but not in browsers where behavior is determined by runtime)
69 * Decompressing response bodies
70
71 Features which don't work with Emscripten:
72
73 * Proxies, both forwarding and tunneling
74 * Customizing TLS and certificates (uses browsers' configuration)
75 * Configuring low-level socket options or source address
76
77 Streaming with Web Workers
78 --------------------------
79 To access the fetch API and do HTTP response streaming with urllib3
80 you must be running the code within a Web Worker and set specific HTTP headers
81 for the serving website to enable `Cross-Origin Isolation <https://developer.mozilla.org/en-US/docs/Web/API/crossOriginIsolated>`_.
82
83 You can verify whether a given environment is cross-origin isolated by evaluating the global ``crossOriginIsolated`` JavaScript property.
84
85 Node.js support
86 ---------------
87 Node.js support uses a relatively new feature in WebAssembly known as JavaScript Promise Integration.
88 To use urllib3 in Node.js, you need to use Node.js version 20 or newer and may need to call Node.js with
89 the ``--experimental-wasm-stack-switching`` command line parameter.