At PyCon 2022, the annual conference for its community of “Pythonistas” – and the first in-person meet-up for Python contributors since 2019 due to the pandemic – developers revisited the idea of running Python code in the browser.
In the browser, Python hasn’t supported compilation to the WebAssembly (Wasm) runtime, a widely supported IC3 web application standard that compiles code written in Rust, C, C++, and Go into a binary format, which allows web apps to act more like a desktop application outside the browser.
But at PyCon 2022, CPython developer Christian Heimes and fellow contributor Ethan Smith detailed how they enabled the CPython main branch to compile to WebAssembly. CPython, short for Core Python, is the reference implementation that other Python distributions are derived from.
SEE: Rocky Linux developer lands $26m funding for enterprise open-source push
CPython now cross-compiles to Wasm using Emscripten, a toolchain that compiles projects written in C or C++ to Node.js or Wasm runtimes.
While the Foundation notes cross-compiling to WebAssembly is still “highly experimental” due to missing modules in the Python standard library, nonetheless, PyCon 2022 demonstrated growing community interest in making Python a better language for the browser.
Some people in the Python community have wondered whether Python has been pulled too far towards data science, potentially undermining its usefulness as a general-purpose programming language. Besides the browser, Python’s other weak point is that running on mobile devices, or indeed any application – even on desktop – requires a graphical user interface.
But even in the data science field, there’s an appetite to make Python more browser-friendly. Peter Wang, co-founder and CEO of Anaconda, the maker of the popular Anaconda Python distribution for data science, previously told ZDNet it was “incredibly awkward to use Python to build and distribute any applications that have actual graphical user interfaces”.
Wang’s firm showed off a new tool they have been working on to bring Python for data science to the browser.
PyScript is built on Pyodide, a port of CPython, or a Python distribution for the browser and Node.js that’s based on WebAssembly and Emscripten.
Pyodide is an interesting development for Python. It was part part of the now-abandoned Mozilla Iodide project and created in 2018 by Michael Droettboom. As of 2021, Pyiodide lives on as an independent, open-source project. It’s is aimed at data science users, enabling statistical and visual modeling work to be done in the browser using key Python data science libraries such as Numpy, Pandas and Matplotlib.
“Pyodide makes it possible to install and run Python packages in the browser with micropip. Any pure Python package with a wheel available on PyPI is supported,” the Pyodide project states. Essentially, it compiles Python code and scientific libraries to WebAssembly using Emscripten.
SEE: Software development is changing again. These are the skills companies are looking for
The Pyodide project released version 0.20 a month ago. It’s been updated to Python 3.10, the newest major release of Python, and brings speed improvements in Firefox and Chrome.
The Python Software Foundation noted: “PyScript is currently built on top of Pyodide, a third-party project bringing Python to the browser, on which work began before Heimes started his work on the CPython main branch. With Heimes’s modifications to Python 3.11, this effort will only become easier.”
“I don’t mind so much different languages have to have different goals. I mean, nobody is asking Rust when you can write Rust in the browser; at least that wouldn’t seem a useful sort of target for Rust either. Python should focus on the application areas where it’s good and for the web that’s the backend and for scientific data processing,” said van Rossum at the time.