7317 PyScript brings Python into the browser, more easily than ever
PyScript brings Python into the browser, more easily than ever

PyScript brings Python into the browser, more easily than ever

❤ 47 , Category: Software,   ⚑

Anaconda, makers of the Python distribution for scientific computing, announced at PyCon US 2022 the PyScript project — an in-browser, single-include way to run Python scripts in HTML pages as easily as JavaScript itself.

Traditionally, Python could interact with JavaScript or a web page only by running as an application server that returned HTML and JavaScript. PyScript has no server component; everything runs directly in the browser.

[ Also on InfoWorld: The rise of WebAssembly ]

Using PyScript, useful components from both the Python and JavaScript worlds can be deployed side-by-side. In one example, the NYC taxi data demo, PyScript uses Pandas to analyze data, then generates an interactive 3D view of the data by way of JavaScript’s WebGL.

pyscript IDG

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 uses a full port of the CPython runtime to WebAssembly to run in-line Python code. You place your Python code inside <py-script> tags, and you can place its output in a selected <div>. A <py-repl> tag inserts a REPL block that allows you to input and execute code, and a <py-env> tag functions like the requirements.txt file in a repository to list needed packages.

The PyScript runtime also enables many common Python packages such as the scientific libraries bundled with Anaconda—NumPy, Pandas, Scikit-learn, and many more. Demos in the PyScript repository show various ways to do this.

While PyScript is still in the early stages, it already provides the ability for Python and JavaScript to interoperate—for Python to reference the DOM, for instance, or for JavaScript objects to address Python objects. PyScript also enables in-browser equivalents for many behaviors needed in a Python distribution, such as listing package requirements for a script.

See also  Entity Framework Core 6 plans take shape

Earlier projects in this vein, like Pyodide (which PyScript uses as a basis for its own work), lacked PyScript’s smooth integration between JavaScript and Python, and its broad access to packages. PyScript has access to every package available through PyPI, with the commonly used Pandas and NumPy already available.

However, third-party packages work best if they’re pure Python. PyScript has no mechanism yet for automatically building platform binaries for packages, although this is theoretically possible. Another obstacle is the time and bandwidth needed to download and set up all of the components needed for a given PyScript-enabled web page. The core Python runtime is about 3.4MB of WebAssembly, although it’s cached after the first load.

[ Keep up with the latest developments in Python and software development. Subscribe to the InfoWorld First Look newsletter ]

Anaconda’s goal for PyScript, as described in a community message board post, is to make it one possible solution to the long-standing problem of packaging and deploying Python applications. It has 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.

Leave a Reply

Your email address will not be published.

five × 2 =