PyScript Vs. Pyodide: Which Should You Use?

Start with PyScript first if you are more of a Pythonic developer. Try Pyodide if you want to “Add some Python” to your JavaScript.

Patrick Chiu
Python in Plain English

--

Disclaimer: I created SkyCube.app which is a Pyodide-powered ML tool that runs entirely in the browser. Also, my opinions (as of 2022/5/10) might be outdated soon, since I have full confidence that Anaconda will advance PyScript to the next level quickly :)

Usage and Aims

pyscript.net

PyScript enables Python applications to leverage HTML, CSS, and JavaScript conventions, but not the other way round yet. What I mean is that you can call JavaScript functions/libraries from Python (PyScript), but you cannot call Python functions from JavaScript. It is understandable as Anaconda’s primary users are Python developers. In fact, one of the aims of PyScript is to help Python take a serious step toward making programming and data science more accessible to everyone.

pyodide.org

On the other hand, Pyodide comes with a robust Javascript ⟺ Python foreign function interface. You can run Python pyodide.runPython("print(x)") or even get a variable pyodide.globals.get("sys") from JavaScript. You can also import js and js.document.title = "New window title" from Python. After all, Pyodide let us freely mix these two languages in your code with minimal friction.

The Bright Side of PyScript

Clean and Simple API

PyScript does provide a clean and simple API! For instance, you can <py-script src="/todo.py"></py-script> to load your Python script. You can even import your other Python script, just like how you normally do so! While in Pyodide, you need to handle the Python script yourself and manage to put it pyodide.runPython(<here>). It is also a great convenience if we just want to output the result to an HTML element by <py-script output="altair"></py-script>, where altair is the id of an HTML element.

Reduced Entry Barrier for Python Beginners

Traditionally (if you are not using Datacamp 😆), you need to at least set up your local environment to start getting your hands dirty with Python, while for JavaScript, you can just right-click and inspect and console.log("hello world!") With PyScript you just need to know very basic HTML to get started, while with Pyodide, you do need to know some more JavaScript to await loadPyodide();

Can We Have the Best of Both Worlds?

Sadly not yet. Once you load PyScript, you cannot execute loadPyodide(); again because Error: Pyodide is already loading. Pyodide is also not in the window object 😭. So currently we have to pick either one.

What I am hoping for?

  1. I do hope that PyScript can somehow let us interface with Pyodide in the future
  2. Better documentation e.g. external file hosting is super cool to me!
  3. More examples or even something like PyFiddle (JSFiddle’s cousin)?

“Clap” if you find this article useful to you :)

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.

--

--

https://patrick-kw-chiu.github.io/ Full stack dev from HK🇭🇰 | Cache Cloud | mongo-http.js | Create tools that may or may not be useful