Website with embedded python code
Executing Python in browser
Python is a programming language that needs no introduction, it’s omnipresent in the tech scene. It is easy to learn due to dynamic typing, versatile to adapt for a programmer, and it’s the first language with which most engineers would have felt extremely comfortable. Though heavily used in the backend for data processing in the software industry, we have massive deployments of Django for web hosting and serving APIs or for automation. Python also has a huge list of packages and scripts to transform data and draw insights via AI/ML.
While browsing a couple of new repos that are gaining traction recently, I discovered a repository called pyscript. The PyScript library provides HTML tags for embedding and executing Python code in your browser. PyScript is built using Pyodide, the WebAssembly port of CPython, which is compiled using Emscripten. It intrigued me because of its wild promises to execute a python code in a browser like chrome and with the ability to import packages and provides a way to execute live code.
I have seen the evolution of Python runtimes over the years, especially how Jupyter notebooks have simplified python development by ensuring reproducibility and the way Google Colab took it to a whole new level by providing a free environment along with the notebooks. But it never occurred to me that something like a PyScript could be there! Before I begin, I want to share my thought on why this could be big, how it works and what are the scenarios where I want to test it.
In my recent experiences, the following became hot topics in the industry and I can confidently say, that if PyScript
lives up to its promises, then it can have a massive impact on the following issues:
- COGS for data crunching
- Today websites need costly servers to power their intensive data operations. Costs for operating a website which needs to crunch a lot of data can go down significantly.
- Privacy
- Doing the data crunching on the user’s device itself is much more secure than sending the data to a server and doing heavy engineering to safeguard the data. Building a website in JS or Node to keep user data on the user’s device will be much more painful.
- Diversity
- HTML is a massively adopted standard. A hosted HTML file can be viewed on any device and in any form factor such as webview or app view.
After realizing the potential, I was extremely excited and I wanted to try this out. So, I made a snippet and hosted it on GitHub pages, pyscripting, source. It looks like this:
<pre><xmp>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sine Wave</title>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<py-env>
- matplotlib
- numpy
</py-env>
<py-script>
import matplotlib.pyplot as plt
import numpy as np
time = np.linspace(0, 2 * np.pi, 100)
plt.plot(time, np.sin(time) )
plt
</pv-script>
<py-repl></py-repl>
</body>
</html>
</xmp></pre>
Now, coming to the scenarios where I want to test PyScript:
localhost
- Currently imported package in<py-env>
are not cached and refetched on every reload, some packages take as much as 40+ ms post reload. This slows down the development since a live reload is practically impossible. Maybe there’s a way to work around this, but it’s not clearly documented.Hosted website
- It works on a hosted website just like GitHub pages. I would assume there would be no blockers unless a network admin specifically chooses to block calls to https://pyscript.net for no good reason.Web speed
- PyScript is terribly slow and doesn’t cache anything. On every reload, it takes ages to initialize the modules, and do a gazillion steps before the page becomes active. Any PyScript website will need users with Everest-level patience.Webviews
- Implementation of webviews is scattered with Facebook implementing its own severely limited version of in-app browser (IAB) and Apple/Google providing its app webview for android apps. I think FB’s IAB (and similar app-specific webviews) will continue to be closed enough to not allow PyScript. But the platform webviews of Apple/Google will allow that, but the performance needs to be thoroughly tested with webviews since each IO will be hardcoded.
TECH
python website browser