PyScript allows you to create rich Python applications in the browser using nothing but HTML and Python code. Although it’s super rough and super new, I can tell you this will be a game-changing part of the Python ecosystem. It’s worth keeping a close watch on this one!
This article explains what PyScript is and how it works. I also included a fully working PyScript REPL for you to play with.
Table of Contents
What is PyScript
PyScript was introduced on PyCon US 2022 by a company well known in the Python world: Anacona. The aim is to allow Python in the context of your browser. PyScript allows you to:
- Write Python code between specific <py-script> </py-script> tags
- Run popular Python packages right in your browser. Some packages that are available include:
- Manage the environment. You can define which packages to include on a page.
- Create visual applications with buttons, text boxes, containers, etcetera.
How does PyScript work?
PyScript runs in your browser, which is pretty amazing! Under the hood, PyScript uses a project called Pyodide. Pyodide is a port of CPython to WebAssembly. This project did not just port CPython to web assembly, but also made the effort to compile well-known packages like Pandas and Numpy so they can be run right from the browser.
In the following screenshot, you can see the actual Python REPL running in my browser:
What does PyScript add to Pyodide?
py-script tags to include Python code in your HTML page.
Another thing I noticed, is that PyScript is trying hard to make Python interoperate with the browser. The Pyodide REPL doesn’t support the
The PyScript REPL
The following REPL runs entirely in your browser, thanks to the awesome PyScript project. PyScript is new and experimental and was only tested in recent browsers so far. However, I’ve found it works in Firefox and anything based on Chrome, like Chrome itself, Chromium, and Microsoft Edge.
Note that this resembles a regular Python REPL, but it’s not at all the same as the real Python REPL. For example, you need to enter your code and hit shift+enter or the play button to run it. It doesn’t support the underscore to get the previous result. It does support the
input() function though!
I’ve embedded the REPL below. If you don’t see anything, it probably means your browser is not supported: