PyScript Makes Web Development Using Python Simpler

PyScript Makes Web Development Using Python Simpler

Anaconda’s new project puts the Python runtime on a web page, using a single JS include and providing access to several popular Python packages.


Anaconda, the makers of the Python package for scientific computing, announced the PyScript project at PyCon US 2022 – an in-browser, single-include approach to run Python scripts in HTML pages as simply as JavaScript itself.

Python could only communicate with JavaScript or a web page in the past by operating as an application server that returned HTML and JavaScript. PyScript does not have a server component; everything is done in the browser.

PyScript allows you to deploy valuable components from both the Python and JavaScript worlds side by side. In one example, the NYC taxi data demo, PyScript analyses data with Pandas before generating an interactive 3D display of the data with JavaScript’s WebGL.

PyScript Demo

“A PyScript demo (real-time rendering of NYC taxi activity) that uses Pandas for number crunching and JavaScript visualization libraries to provide an interactive view of the data.”

PyScript runs in-line Python code using a full port of the CPython runtime to WebAssembly. You insert your Python code inside py-script> tags, and the output is placed in a div> of your choice. A py-repl> tag inserts a REPL block that allows you to enter and execute code, while a py-env> tag operates similarly to a repository’s requirements.txt file to list required packages.

Many common Python packages, such as the scientific libraries included with Anaconda—NumPy, Pandas, Scikit-learn, and many others—are also supported by the PyScript runtime. Demos in the PyScript repository demonstrate several approaches.

While PyScript is still in its early stages, it already allows Python and JavaScript to interact, allowing Python to reference the DOM, for example, or JavaScript objects to address Python objects. PyScript also provides in-browser counterparts for several Python distribution-required actions, such as reporting package dependencies for a script.

Earlier projects in a similar vein, such as Pyodide (which PyScript uses as a foundation for its work), lacked PyScript’s seamless integration of JavaScript and Python, as well as its extensive package support. PyScript offers access to any package accessible through PyPI, including the widely used Pandas and NumPy.
Third-party packages, on the other hand, operate best if they are pure Python. PyScript currently lacks a mechanism for automatically generating platform binaries for packages, while this is theoretically conceivable. Another barrier is the time and bandwidth required to download and install all of the components required for a certain PyScript-enabled web page. The main Python runtime is around 3.4MB of WebAssembly, however, it is cached after the initial load.

According to a community message board post, Anaconda’s goal for PyScript is to offer one feasible answer to the long-standing challenge of packaging and distributing Python programs. It has long been difficult to package up a Python script, especially one with many dependencies, for someone else to use as-is without the Python runtime.

Dimension VIZ creates unique, hardworking websites that you can be proud of as the online face of your company. For optimal impact, we focus on three important website design elements: visibility, usability, and converting visitors into buyers.

Because we design websites with the most up-to-date trends and technologies, your consumers will always have a smooth, fluid, and user-friendly experience, whether they’re on a desktop, laptop, tablet, or phone. This implies you’ll have happy visitors who stay on your website rather than moving on to the competitions.

Leave a Comment

Your email address will not be published.