Zephyrnet Logo

PyScript: Python in the Web Browser

Date:

image

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:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello, World!</title>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<py-script>print("Hello, World!")</py-script>
</body>
</html>
</pre>

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.

spot_img

Latest Intelligence

spot_img