fbpx
Python

Want to run Python code in a browser? Soon you might be able to

Python might be the most popular programming language in the world, but unlike other JavaScript frontrunner, you can not run Python code in the browser.

At PyCon 2022, the annual conference for its community of “Pythonistas” – and the first in-person meet-up for Python contributors since 2019 due to the pandemic – developers revisited the idea of ​​running Python code in the browser.

In the browser, Python hasn’t supported compilation to the WebAssembly (Wasm) runtime, a widely supported IC3 web application standard that compiles code written in Rust, C, C ++, and Go into a binary format, which allows web apps to act more like a desktop application outside the browser.

But at PyCon 2022, CPython developer Christian Heimes and fellow contributor Ethan Smith detailed how they enabled the CPython main branch to compile to WebAssembly. CPython, short for Core Python, is the reference implementation that other Python distributions are derived from.

SEE: Rocky Linux developer lands $ 26m funding for enterprise open-source push

CPython now cross-compiles to Wasm using Emscripten, a toolchain that compiles projects written in C or C ++ to Node.js or Wasm runtimes.

The Python Software Foundation highlighted the work in a blog post: “Python can be run on many platforms: Linux, Windows, Apple Macs, microcomputers, and even Android devices. But it’s a widely known fact that, if you want code to run in a browser, Python is simply no good – you’ll just have to turn to JavaScript, “it notes. “Now, however, that may be about to change.”

While the Foundation notes cross-compiling to WebAssembly is still “highly experimental” due to missing modules in the Python standard library, nonetheless, PyCon 2022 demonstrated growing community interest in making Python a better language for the browser.

Some people in the Python community have wondered whether Python has been pulled too far towards data science, potentially undermining its usefulness as a general-purpose programming language. Besides the browser, Python’s other weak point is that running on mobile devices, or indeed any application – even on desktop – requires a graphical user interface.

But even in the data science field, there’s an appetite to make Python more browser-friendly. Peter Wang, co-founder and CEO of Anaconda, the maker of the popular Anaconda Python distribution for data science, previously told ZDNet it was “incredibly awkward to use Python to build and distribute any applications that have actual graphical user interfaces”.

Wang’s firm showed off a new tool they have been working on to bring Python for data science to the browser.

Wang announced PyScript at PyCon 2022, describing it as “a system for interleaving Python in HTML (like PHP).” It allows developers to write and run Python code in HTML, and call Javascript libraries in PyScript. This system allows a website to be written entirely in Python.

PyScript is built on Pyodide, a port of CPython, or a Python distribution for the browser and Node.js that’s based on WebAssembly and Emscripten.

Pyodide is an interesting development for Python. It was part of the now-abandoned Mozilla Iodide project and created in 2018 by Michael Droettboom. As of 2021, Pyiodide lives on as an independent, open-source project. It is aimed at data science users, enabling statistical and visual modeling work to be done in the browser using key Python data science libraries such as Numpy, Pandas and Matplotlib.

“Pyodide makes it possible to install and run Python packages in the browser with micropip. Any pure Python package with a wheel available on PyPI is supported,” the Pyodide project states. Essentially, it compiles Python code and scientific libraries to WebAssembly using Emscripten.

SEE: Software development is changing again. These are the skills companies are looking for

The Pyodide project released version 0.20 a month ago. It’s been updated to Python 3.10, the newest major release of Python, and brings speed improvements in Firefox and Chrome.

The Python Software Foundation noted: “PyScript is currently built on top of Pyodide, a third-party project bringing Python to the browser, on which work began before Heimes started its work on the CPython main branch. With Heimes’s modifications to Python 3.11, this effort will only become easier. “

At PyCon 2021, Python’s creator, Guido van Rossum, acknowledged Python was great for back-end web development, but conceded the front-end to JavaScript. Why overstretch Python in well-served fields by other languages ​​when Python can focus its strengths in data science?

“I do not mind so many different languages ​​have to have different goals. I mean, nobody is asking Rust when you can write Rust in the browser; at least that would not seem a useful sort of target for Rust either. Python should focus on the application areas where it’s good and for the web that’s the backend and for scientific data processing, “said van Rossum at the time.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button
KQ Education Group