Intro slide: Hi, I'm Matjaž Drolc. I'm working on software for optimizing the maintenance of roads. We gather the data from measurements of road surfaces and do some magic to predict how the asphalt is going to perform in the future. The data that we get is not perfect. To obtain good results we need a way to see what is happening. This is why we started using Python. Tools such as Pandas and Jupyter enable us to have fast feedback during development. [show Jupyter notebook]
However, things are changing.
The hell froze in 2017. Developers of Chrome, Edge, Firefox, and Safari started talking to each other after more than a decade of incompatible web browsers. They announced a thing called WebAssembly. WebAssembly is a new format for distributing program code as part of web applications.
[Open HelloWorld in WASM explorer https://mbebenita.github.io/WasmExplorer/]
The main point here is that we now have open doors to run whatever programming language we want in a web browser. We just need a compiler. For C and C++ we have a suite of compiler and tools called Emscripten. It has existed for many years now and is quite stable. Rust compiler is also quite mature and people are also working on compilers for Go, Kotlin and .NET languages (C#, F#...).
Does anybody know which important piece of software is written in C? It's the official Python interpreter - CPython.
Pyodide is a project that aims to bring the Python scientific stack to the web browser. So far they already ported Python interpreter, Numpy, Scipy, Pandas, matplotlib and other libraries.
Both Jupyter and Pyodide look similar on the surface. Developer tools (F12) reveal the most significant difference.
Each time I execute some code in Jupyter, that code is sent to the server, executed there and the result is sent back to the browser. With Pyodide there is no communication with the server. Everything, including Python interpreter, runs in the browser. And we are talking about official Python interpreter, built from the same source code, that you use elsewhere.
[Show demo with developer tools on the network tab and Jupyter] [Show demo with developer tools on the network tab and Pyodide]
We can get more advanced and combine several mouse events to produce a simple app for doodling.
With a call to pyodide.pyimport(name) we can access any Python object. Here we first create a class called Foo and create an instance called foo. Then we request that object from JS side and read the 'val' attribute.
The reason I use Python are mostly its scientific libraries. Pyodide includes many packages, including Numpy, Pandas, SciPy and matplotlib. Here is a tiny numpy example. First we calculate values of sine function and then we display those with matplotlib. You can see a list of all ported packages on Github. https://github.com/iodide-project/pyodide/tree/master/packages
And to make things crazy, they also ported WebM video codec to WebAssembly. There are no obstacles to making music or a video editor in a web browser.
We are also getting some new games. Unity and Unreal Engine both support WebAssembly. DEMO: https://s3.amazonaws.com/mozilla-games/ZenGarden/EpicZenGarden.html
Other important WASM projects:
- https://wasi.dev/ - WebAssembly System interface. Currently, the way that WebAssembly code communicates with the system around it is not standardized. People who write compilers created some ad-hoc solutions. But WASI project is aiming to standardize this.
- https://wasmer.io/ is an universal WebAssembly runtime. They are trying to run WebAssembly outside of web browser. For example on servers.
- https://github.com/mohanson/pywasm is a WebAssembly interpreter written in pure Python. If you want to run your Python code in Python that runs in WebAssembly that runs in Python, you can.
I think the future will be interesting. In the near term, we will see better viewers of Jupyter notebooks and more web apps where we can play with data. We might see improvements to Python documentation. The path is open to make it richer by embedding interactive code snippets. We will also see the transition of software, that was traditionally running on the desktop, to the web. For example, software for video/audio editing, CAD, scientific computation...