Pyodide Notes
Some unorganised notes on Pyodide from my recent experimentations with it
Pyodide Notes
I have attempted a few projects using Pyodide, mainly to learn how to use it for personal projects and run Python scripts in-browser. Most of my projects have not found much use for the intercommunication between Python and JavaScript that Pyodide provides.
What is Pyodide
I doubt I will fill this section all too thoroughly, as it is likely in the README of one of my projects, but Pyodide allows users to run Python in their browser.
Pyodide is a tool that allows you to run Python in your browser using Web Assembly, a port of CPython to WebAssembly / Emscripten. Somewhere within the Pyodide project is a compiled .wasm Web Assembly binary which is the Python interpreter compiled to WASM.
The most common way to import the Pyodide script is via CDN either using a dyamic import and the URL, or via a <script> tag <script src="https://cdn.jsdelivr.net/npm/pyodide@0.28.0/pyodide.min.js"></script>. This is an older style of import that uses an Immediately Invoked Function Expression (IIFE) and attaches loadPyodide to window - rather than a modern ECMAScript Module (ESM) which would allow you to import code without any invoked functions, and without attachments to window.
What is Web Assembly
From Wikipedia: WebAssembly defines a portable binary-code format and a corresponding text format for executable programs as well as software interfaces for facilitating communication between such programs and their host environment.
Resetting Pyodide between Runs
For the use cases I have for Pyodide I usually want every press of “run” to essentially start fresh, with no carry-over from previous runs.
There seem to be a few methods suggested out there, and I have not tested which one is perfect for removing “artifacts” between runs.
One method is to run Python itself to clear globals and sys.modules, calling pyodide.runPython on the code below. The code is not comprehensive, and is more a suggestion, in practice you may end up accidentally clearing globals and sys.modules that Pyodide relies upon.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import sys
import gc
# Clear globals except built-ins and sys itself
for key in list(globals().keys()):
if key not in ('__builtins__', 'sys'):
del globals()[key]
# Remove all modules except built-ins and sys
for mod in list(sys.modules.keys()):
if mod not in ('sys', 'builtins'):
del sys.modules[mod]
# Force garbage collection
gc.collect()
Another method is to attempt to create a completely new Pyodide instance. It remains to be seen how effective this is at clearing globals and sys.modules, but if it does actually work for a reset then it is definitely the preferred method of resetting Pyodide.
1
2
pyodide = null;
pyodide = await loadPyodide();
Imports in Pyodide
Imports in Pyodide work for a lot of the standard library modules, and also for a few other packaged modules that Pyodide provides by default. You can install your own packages to Pyodide via .whl, or via micropip, or within executing Python scripts, even via URL.
Two files trying to interact to import from each other is best handled via pyodide.FS the filesysten of Pyodide.