PyScript is an open-source platform designed to bring Python into modern web browsers, aiming to combine the familiarity and accessibility of the web platform with the expressiveness and depth of one of the world’s most popular programming languages. The goal is to allow developers to build rich web applications using Python’s extensive ecosystem of libraries, without sacrificing the native features of the browser.
Interpreters and DOM Interaction
To operate, PyScript relies on two Python interpreters compiled into WebAssembly (WASM), which users can choose based on their specific needs: Pyodide and MicroPython. The former is a full-featured and robust version of the standard CPython interpreter. It includes the most popular libraries and many core packages like NumPy, Pandas, SciPy, and Matplotlib, making it the ideal choice for complex or data science-related applications. The latter, by contrast, is a leaner and more efficient version, optimized to run in resource-constrained environments, making it particularly well-suited for mobile and tablet browsers.
The system interacts with the browser’s Document Object Model (DOM) in two main ways: via the Foreign Function Interface (FFI), which creates Python proxy objects linked directly to JavaScript ones, and through the pyscript.web module. The latter is a “Pythonic” API, available only in the most recent versions of PyScript, which introduces the global page object to represent the current web page. This allows developers to select HTML elements using square-bracket syntax like page["#id"] or classic CSS selectors via .find(). The returned elements expose more intuitive properties such as styleclasses, as well as methods like .append().
from pyscript import when
from pyscript.web import page
input_data = page["#input-user"]
button = page["#my-button"]
output_box = page["#result"]
PyScript Project Structure
A PyScript project essentially relies on three components:
1. An index.html file that defines the page structure and calls the PyScript module:
2. A configuration file (pyscript.json o pyscript.toml) to describe the environment and the required packages (the three.js module has been included here as an example):
name = "Threejs esample with PyScript"
description = "An example on how to use Three.js and WebGL in PyScript."
[js_modules.main]
"https://cdn.jsdelivr.net/npm/three@0.181.2/build/three.module.js" = "three"
{
"name": "Threejs example with PyScript",
"description": "An example on how to use Three.js and WebGL in PyScript.",
"js_modules": {
"main": {
"https://esm.sh/three@0.181.2": "THREE"
}
}
}
3. A main.py file containing the application’s main Python code:
from pyscript import when, window, document
from pyscript.js_modules import THREE
import numpy
Areas of Application
The potential of PyScript spans several areas, such as data analysis and visualization—where the integration of libraries like Pandas and Matplotlib makes it highly effective for creating complex dashboards and charts directly on the web; the development of simple web applications or 3D graphics scenes using libraries like Three.js (albeit with some limitations); and last but not least, education, since Python is an easy language to teach and learn.
Current Advantages and Limitations
This solution therefore seems to offer numerous advantages, starting with its ease of use: it requires no complex infrastructure setup and allows code execution directly within the user’s browser. Furthermore, it is a universal and secure solution, applicable anywhere a modern browser is available by leveraging its security sandbox. Finally, it enables Python programmers to utilize their skills and the vast ecosystem of available libraries, extending the language’s capabilities into the web context.
However, as a project still in its early stages, PyScript presents significant challenges. Many features are still a work in progress, making the development of complex applications difficult. The lack of a UI framework is a major drawback; in fact, there is currently no true Python equivalent to modern JavaScript frameworks (such as React or Vue) for managing more elaborate user interfaces. Another disadvantage we experienced firsthand is its incompatibility with ES Modules, which prevents the proper use of essential external components, such as loaders for 3D models (GLTF) or textures in Three.js.
Conclusion
The consensus is that PyScript is not yet intended to replace traditional JavaScript frameworks for large-scale application development. However, it represents an increasingly compelling solution for dashboards, scientific tools, prototypes, and applications where Python is already the primary language. The project enjoys the backing of major entities like Anaconda, and the qualitative improvements between versions are evident. Furthermore, the community is highly active on platforms like Discord and GitHub, signaling growing interest and vibrant development. For those wishing to experiment with this technology, the pyscript.com platform offers a simple and immediate environment to develop and host web applications. Looking ahead, PyScript represents a promising evolution for Python’s web integration and, if development continues at this pace, it could revolutionize the future of web interoperability.
