PyScript: Python In The Web Browser

A chainsaw can make short work of clearing out the back forty. It can also make a good horror movie. So while some people will say we don’t need another tool to allow more malicious scripting in the browser, we also know that, like any tool, you can use it or abuse it. That tool? PyScript, which is, of course, Python in the browser.

The tool is in the early experimental phase, so the project doesn’t suggest using it in a production environment yet. However, if it works well, the promise is not just that you can write browser-based applications in Python — you’ll have a handy way to reuse existing Python code and even be able to run the same code on the browser that currently runs on the server. This has a lot of implications for improved client/server applications, or cases where you want to be able to run against a local backend when disconnected and a remote backend when you do have a connection. Of course, you can interoperate with JavaScript, too.

However, the real goal is to make web-based programming accessible to beginners in the same way as programming systems like Scratch or JSFiddle do. As such, the actual project is less of a piece of software and more of an integration between existing pieces. According to the post:

PyScript is a single-page application (SPA) written in TypeScript using the Svelte framework, styled with Tailwind CSS, and bundled with rollup.js.

PyScript wouldn’t be possible without building on top of a recent version of Pyodide—a CPython interpreter compiled with emscripten to WebAssembly, enabling Python to run in the browser. PyScript provides a thin abstraction layer over Pyodide by encapsulating the required boilerplate code, which you’d otherwise have to type yourself using JavaScript.

So how hard is it to create PyScript code? Not very:

What’s interesting is that this requires no server set-up whatsoever. Save that text to a file, open it in a browser and it runs. Well, of course, no server set-up for you. Presumably, the work is being done on the pyscript.net server which takes a lot of set-up! There’s also a bit of load time for something this simple, as you might expect, especially on the first load. You can, however, host your own server.

Some people will love this, and others will hate it. Either way, it is here, and if you have to script in the browser, you could do worse than Python. We are still holding on to Fortran, though maybe we should upgrade to Forth.

25 thoughts on “PyScript: Python In The Web Browser

  1. *sigh* The ability to specify the language (per the “type” attribute) in the “script” tag is part of the official HTML. But nooOOOOooo, they just had to go and create an entirely different tag. -_-

  2. Would python be able to record and then replay/complete repetitive tasks on a website (just simple tasks, like clicking, selecting boxes, entering same data)? Do you know if there is a script for this?

    1. Not sure about python, but it sounds like you are looking for a E2E testing suite. Check out Selenium and/or Cyprus. There are other options as well, but those seem to be the top ones currently.

      1. Thank you, I am currently using Katalon Recorder (free) as a Chrome extension. Works very well for my simple needs at this time, but I am looking for something that I own and control because any day that company can modify/yank the product.

  3. If the Python-code can interact with JavaScript-code, this could also be a means for someone to port their Python-code to JavaScript/WebAssembly/whatever in bite-sized snippets: take a portion of your Python-code, comment it out and implement it in JavaScript, then call the JavaScript-implementation instead and test that everything works as supposed to. Rinse and repeat until all the code has been ported over.

    It’s not necessarily the most efficient approach, but if you’re less experienced with JavaScript, it could be a great help.

    1. I think that’s what is happening here. It seems to me that PyScript leverages Pyodide which is an online Python console. I’m pretty sure Pyodide is built on Javascript and WebAssembly.

  4. “On a slightly more technical level, PyScript is a single-page application (SPA) written in TypeScript using the Svelte framework, styled with Tailwind CSS, and bundled with rollup.js. ”

    So…. we’re going to drag web performance down to a whole new low because of “beginners”.

    Uhuh.

    1. From what I understand, its not about the beginners. I think (and I hope) its actually about all the libraries/modules already made for python and running them on browsers. From my short test with PyScript, if you keep the python code to a minimum, and do as many heavy computation with libraries that are compiled (like numpy) then the performance hit is not too bad. I do still think this is a terrible idea, and the amount of popularity its getting is kinda scary.

  5. To me PyScript seems to encapsulate Python in HTML that then uses the separate online Pyodide Python Console for execution. Plus Real Python where PyScript seems to come from keeps trying to sell me books about learning Python. I get where PyScript is coming from, execute Python in your markup code via a cloud Python console. But personally I would just run Java or Javascript instead.

    However if you really want to run Python in your browser there are some good options…

    There’s Online Python which allows you to paste your source and run it like you’re in an IDE. It works great where you need to develop with source files.[1]

    Then there’s Pyodide which is an interactive Python console that lives in your browser.[2] You can work directly with Pyodide online, you do not need something like PyScript to use it.

    If you need to get deep into Python via the cloud/browser, there’s the venerable Jupyter project.[3] Jupyter is great for collaborative development. But for me, Jupyter is usually too much for what I’m doing. It is powerful though. The concept is that you develop and deploy via instances called Jupyter Notebooks. You can use Jupyter via the browser only or install JupyterLab locally via Python’s pip.

    There are more online Python options, I just picked some that I have tried.

    * References:

    1. Online Python

    https://www.online-python.com/

    2. Pyodide

    https://pyodide.org/en/stable/

    https://github.com/pyodide/pyodide

    This is where you get the interactive console. Enable scripting in your browser. Once you point your browser to this URL, give it some time to load and run. It doesn’t take very long to load and once it’s up it is quite snappy:

    https://pyodide.org/en/stable/console.html

    3. Finally, here’s Jupyter

    https://jupyter.org/

    1. I suspect that the experience with PyScript will be closer to normal Python, since with that you’re running the actual CPython 3.10 interpreter (compiled to wasm) and you can use pure python packages from PyPI plus a number of the typical data science packages (numpy, scikit learn, pandas etc full list here: https://pyodide.org/en/stable/usage/packages-in-pyodide.html)

      If that doesn’t matter to people then they probably can use pyjs but i note the repo hasn’t been updated in almost seven years (the descriptions on their site gave it away with reference to slightly dated tech)

  6. I’ve been using Anvil.works, which is a complete open source full stack web app suite. Write client and server code in Python without HTML, CSS, JS or SQL. It’s a commercial operation, but they’ll host small projects for free.

Leave a Reply to SamCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.