PyScript: Python in the web browser

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

The tool is in the early experimental phase, so the project does not yet suggest using it in a production environment. However, if it works well, the promise isn’t just that you can write browser-based applications in Python – you’ll have a convenient way to reuse existing Python code and even be able to run the same code on the browser that is currently running. on the server. This has many implications for enhanced client/server applications, or cases where you want to be able to run a local backend when disconnected and a remote backend when connected. Of course, you can also interact with JavaScript.

However, the real goal is to make web programming accessible to beginners in the same way that programming systems like Scratch or JSFiddle do. As such, the actual project is less software than an integration between existing elements. 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 relying on a recent version of Pyodide, a CPython interpreter compiled with emscripten for WebAssembly, allowing Python to run in the browser. PyScript provides a thin layer of abstraction over Pyodide by encapsulating the required boilerplate code that you would otherwise have to type in yourself using JavaScript.

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

What is interesting is that it does not require any server configuration. Save this text to a file, open it in a browser and it runs. Well, of course, no server configuration for you. Presumably the work is done on the pyscript.net server which takes a lot of configuration! There’s also a bit of a load time for something so simple, as you’d expect, especially on the first load. However, you can host your own server.

Some people will like it, and some people will hate it. Anyway, it’s here, and if you have to script in the browser, you could do worse than Python. We still hang on to Fortran, though maybe we should switch to Forth.

Comments are closed.