Python Mercenaries
in
t
g
p
b
f

An Introduction to PyScript

A new framework that brings the Python language into HTML and the web browser

written by Chris Leonard on 2022-09-06

PyScript Introductio graphic

On April 30, 2022, Peter Wang, CEO of Anaconda, introduced PyScript, a new framework that brings the Python language into HTML and the web browser. The framework allows developers to create Python applications directly in the browser without requiring a server backend.

<html>
<head>
...
# Include PyScript CSS and JS
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
...
<py-script>
print("Hello world!")
</py-script>
...
</html>

The PyScript project offers many important features and benefits beyond the ability to author Python in HTML documents:

PyScript has been billed as a lot of things such as "programming for everyone", a JavaScript killer (it's not), or a replacement for Jupiter notebooks (again, it's not). We would like to focus on some less often mentioned benefits of PyScript for practitioners, data science teams, academics, and business leaders.

Stability

After many years of chaos and growing pains including the browser wars, the dot com bubble, and the development of Web 2.0, the web has stabilized into a rock-solid platform. The web is itself cross-platform and backwards compatible. Sites that ran 10 years ago will still run today. While the PyScript project is still new, it is also quickly maturing and stabilizing. One might argue that, in the near future, PyScript will become the most reliable way to create and distribute data-centric projects.

All of the Packages (well, lots of them...)

PyScript is based on the Pyodide project. Pyodide was built to include a large number of packages covering many of the disciplines for which Python is often used:

See Packages built in Pyodide for more.

Sharing and Collaboration

Since PyScript projects are simply HTML documents, sharing complex applications becomes far more simple than the sharing of a traditional project. Want to show off your work to a colleague? You can email the file or drop it into Slack. You can run a local server (python -m http.server) and use something like ngrok to send a public URL to your friend. No spinning up an environment or virtual machine.

Deployment

Ready to publish your project? Deployment is as easy has hosting a static website. No devops, no Linux administration, no Docker or Kubernetes. If you can host a simple "Hello World" web page, you can publish a PyScript project.

View Source

By bringing Python into the browser, the "how did they do that" of a data-centric app becomes as accessible as clicking "View Source" in the browser. Before YouTube, blogs, and bootcamps, early web practitioners, such as myself, learned our craft by viewing the source of other people's work. In fact, I would personally credit the ability to view the source of any website as a key contributor to the rapid growth of the early web. The ability to view the source of PyScript projects is huge.

Additional Reading


« Previous | An Introduction to PyScript