Dear Fellow Anvilistas,
Please do not read on, unless you’re interested in some a somewhat exotic and mostly failed experiment…
I’ve been looking into Pyodide the past few days…. Like Skuplt, Pyodide seeks to “bring Python to the browser” but unlike Skulpt Pyodide does so with Web Assembly (WASM). My motivation for playing around with Pyodide was NOT as an alternative to Anvil. On the contrary, my ambition was to broaden the use case for Anvil in two ways:
-
Making any Python package available client-side from within Anvil.
-
And for me, more importantly, making it possible to run Anvil Uplink (client) Python code from any JavaScript application. Specifically, I would have loved to be able to write an application where all the UI stuff was written in vanilla HTML, CSS and JavaScript, but where all data processing and server communication were handled with client-side Python code using Anvil Uplink (in a Pyodide web worker).
I tried #2 (using Pyodide’s micropip) from both a regular JS script and by using a web worker, but in both cases I got an error message about a ‘missing _ssl’. For me, working with Pyodide is enough black magic as it is, so I’ve (at least for now) given up… But it would have been so sweet to be able to take advantage (in any JS application) of Anvil’s great client-server communication instead of dealing with HTTP endpoints and fetch (I’ve not linked to this failed attempt here).
Concerning #1, I used the Python package, Pint (an excellent package for unit conversion) as a test case. Before a tighter integration into Anvil, I initially attempted to simply call an index.html file from a startup module (‘window.location = …’; essentially using Anvil as a launchpad for a vanilla web application). When I clone the Anvil app repo locally and run the index.html file directly from VS Code, everything actually works: A small Python script (awkwardly placed in an Assets JS file as a JS string) does its magic.
However, when I do the same thing from Anvil, things break down with a “no native wasm support detected” error in the console. I’m really punching far above my weight here, but perhaps it has got something to do with the way Anvil wraps scripts in service workers(?). Here’s a link to the Anvil app:
https://anvil.works/build#clone:IISGY5LWZOO7UJNR=GT5OEXPMY3QCV5G7FPKYXDYR
Long story short, if you want to run a working version of this experiment, do the following:
-
Clone the repo locally.
-
Open the local repo in e.g., VS Code and run the index.html file (e.g., with the Live Server VS Code plugin).