With PyScript, Python code runs in a browser
With PyScript, the latest project from Anaconda, it is possible to run Python code in a web page, using a single JS include string and access many popular Python packages.
Announced during the PyCon US 2022 conference organized from April 27 to May 3 by the editor of the Python distribution for scientific computing Anaconda, the PyScript project makes it possible to execute Python scripts in HTML pages as easily as JavaScript itself. same. Traditionally, Python could only interact with JavaScript or a web page by running as an application server that returned HTML and JavaScript. The interest of PyScript is that it has no server component, since everything runs directly in the browser. Thanks to PyScript, useful components from both the Python and JavaScript worlds can be deployed side by side. For the NYC taxi data demo made at the conference, PyScript used Pandas to analyze the data, then generated an interactive 3D view of the data via JavaScript’s WebGL.
PyScript uses a full port of the CPython runtime to WebAssembly to execute Python code inline. Just put the code in the tags and put the output in a select. Then a tag inserts a REPL block so you can enter and run code, and a tag works like the requirements.txt file in a repository to list needed packages. The PyScript runtime also enables many common Python packages, including scientific libraries like NumPy, Pandas, Scikit-learn, and many more that come with Anaconda. Demos from the PyScript repository show different ways of doing this.
Interacting Python and JavaScript
Even though PyScript is still in its infancy, it already allows Python and JavaScript to interact with each other, for Python to reference the DOM, for example, or for JavaScript objects to address Python objects. PyScript also offers in-browser equivalents of many behaviors needed by a Python distribution, such as listing package requirements for a script.
Earlier projects, such as Pyodide (which PyScript uses as the basis for its own operation), did not offer the smooth integration that PyScript does between JavaScript and Python, nor did it offer broad access to packages. PyScript has access to all packages available through PyPI, including the commonly used Pandas and NumPy packages already available. Note, however, that third-party packages work best if written in pure Python. Although theoretically possible, PyScript does not yet contain a mechanism to automatically create platform binaries for packages. Another hurdle is the time and bandwidth it takes to download and configure all the necessary components for a given PyScript-enabled web page. The main Python runtime is equivalent to a 3.4 MB WebAssembly (wasm) binary, although it is cached after the first load.
Respond to the challenge of packaging and deploying Python applications
The goal of Anaconda for PyScript, as described in a community forum post, is to make it a possible solution to the long-standing problem of packaging and deploying Python applications. It has indeed long been difficult to take a Python script, especially one with many dependencies, and bundle it up for someone else to use as is without the Python runtime.