PyScript: Python in the web browser

A chainsaw can do a brief job of cleaning the back forty. It can also make a good horror movie. So while some people will say that we don’t need any other tools to allow more malicious scripting in the browser, we also know that, like any tool, you can use or abuse it. That tool? PyScript, of course, Python in the browser.

The tool is in the early testing phase, so the project still does not recommend using it in a production environment. However, if it works well, the promise is not just that you can write browser-based applications in Python – you will have an easy way to reuse existing Python code and even be able to run the same code in the current browser. / It has many implications for server applications, or in cases where you want to be able to run against a local backend if you are disconnected and against a remote backend if you have a connection. Of course, you can interact with JavaScript.

However, the real goal is to make web-based programming accessible to newcomers, just like programming systems like Scratch or JSFiddle. 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 would not be possible without a build on the latest version of Pyodide CP a CPython interpreter compiled with WebSssen with emscripten to enable Python to run in browsers. PyScript encapsulates the required boilerplate code and provides a thin abstract layer on the pyrid, which you would otherwise have to type using JavaScript.

So how difficult is it to create PyScript code? Not much:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello, World!</title>
<link rel="stylesheet" href="" />
<script defer src=""></script>
<py-script>print("Hello, World!")</py-script>

The funny thing is that no server setup is required for this. Save that text to a file, open it in a browser and run it. Well, of course, set up a server for you. Most likely, the work is being done on server which takes a lot of set up! There is also some load time for this simple thing, as you might expect, especially in the first load. However, you can host your own server.

Some people will like it, and others will hate it. Either way, here it is, and if you have to script in a browser, you can do worse than Python. We’re still holding on to Fortran, though we should upgrade to Forth.

Leave a Reply

Your email address will not be published.