Data Science / Frontend Development / Software Development

Pyscript: A Browser-Based Python Framework for the 99%

8 Jul 2022 7:55am, by
Pyscript: A Browser-based Python Framework for the 99%

Raphael Madu, who programs in machine language and Python, isn’t sure he sees the point of Pyscript, the new Python framework being developed by Anaconda.

It’s interesting, he told The New Stack, but the mechanical engineering intern didn’t see the point despite having tried it and written a short tutorial on it.

“It seems it has a lot of potential,” he said. “But for now, there’s nothing really from Pyscript right now that I’ve seen.”

He’s not the only programmer who doesn’t see the point — and that’s okay, said Anaconda co-founder and CEO Peter Wang. Pyscript is not trying to convert professional web developers. Instead, it’s designed for the 99% of web users who aren’t professional developers, Wang said.

“The biggest thing I would say to people who are like, ‘I don’t see why this is necessary,’ is simply [that] it’s not about you,” he said. “It’s for all of the schoolchildren that you don’t spend your afternoons trying to teach how to build their first application.”

Inspired by HyperCard

To teach a child how to program today is complicated. Beyond teaching them programming, you’d also need to teach them how to bundle, about web server architecture and cloud deployment. You’d “spend three days teaching them that before they can do a ‘Hello World’ app,” Wang said.

Besides a strong sense of justice, what inspired Wang was his own early experience growing up with HyperCard, a pre-web development kit for the Macintosh that included a scripting language.

“HyperCard was about my second or third programming language or environment, and I still remain very inspired by how simple it was for a child — I was a child at the time — to start putting together real applications,” Wang said.

During his long tenure with Python, he’s seen many people try Python on as their first language. He empathizes with these novices and citizen developers, and hopes Pyscript will provide them with a way to quickly use Python for their first “Hello World.”

How Pyscript Works

Python is easy to learn, but it’s challenging to write and distribute Python applications with a user interface, LWN.net pointed out recently. Even using a web frontend requires writing JavaScript, CSS and HTML, Wang told LWN.

It turns out, the key to deploying Python is WebAssembly, Wang explained. Last year, Anaconda began to explore the work being done with Python and WebAssembly — projects like Pyodide and JupyterLite, which have compiled parts of the Python scientific and numerical stack to target WebAssembly.

“We were intrigued by some of the underlying work that was starting to happen around Python and WebAssembly,” he told The New Stack. “Once the thing in my head clicked, that it’s not just about having a little Python terminal embedded in the browser, running WebAssembly, but actually, let’s open up the entire browser environment to all of Python and all the Python data stuff, then just a lot of things started cascading from that.”

WebAssembly is a W3C standard supported by browsers, including mobile browsers. As Wang explained to LWN.net, WebAssembly is a virtual CPU instruction set with a 32-bit address space; and it can do 64-bit arithmetic. The compiler, Emscripten, can be used to compile most C and C++ code to WebAssembly. Because CPython is a C program, much of the Python numerical stack is written in C or C++, he said.

It seemed to Wang that with some bindings and “syntactic sugar,” Python — not a variation of it, but the whole of it — could run in the browser without sacrificing compatibility with the rest of the ecosystem’s components. So Pyscript evolved from there.

“We were intrigued by some of the underlying work that was starting to happen around Python and WebAssembly.”

Anaconda CEO Peter Wang

“The difficulty with Python versus any other language is that a lot of Python’s power comes from its usage for data analysis and numerical computing, and that requires a very large, extensive and difficult to compile set of C and C++ and Fortran libraries or packages, we might call them,” he said. “So part of the big lift that only recently happened was [that] a lot of those libraries were ported to run on WebAssembly.”

That made it possible for Python and its data extension system to run directly in the browser as a first-class citizen alongside something like JavaScript, he said.

That big change made Pyscript possible. Anaconda introduced some additional opinionated framework bits, he said, with the vision that someone would be able to write an HTML tag, insert some Python code, and then close out the tag and it would simply work within the browser.

Right now, to run it, the code hits Anaconda’s servers to actually load the Pyscript runtime and packages. Once those are loaded into the user’s browser, however, there is no longer the need to connect to the server — it runs entirely locally in the browser, he explained.

“If you’re a developer, and you want to use Pyscript to ship your app, you would simply put the Pyscript artifacts, the runtime and the modules on your server,” he said. “When the user came to your server, they would then load it from you and then they will be running inside their browser tab.”

It’s actually a peer-to-peer application distribution mechanism that allows people to share applications with each other, he explained

”If they were in a completely disconnected environment, they would still be able to run full-fledged, bonafide web applications with all the Python stack, and they wouldn’t have to hit anyone’s server,” he said. “So in a way, it sort of points to a capability for Web3, but using Python to do the Web3 development.”

Pyscript is in alpha and is not production-ready, but Wang did feel it was ready enough to demo during his keynote at PyCon 2022.

What’s Next for Pyscript

Pyscript is still in alpha, but Anaconda plans to improve the speed of package loading and the size of the download, to make it snappier. It’s also working on increasing support to make it easy to build user interfaces with it. The team is also working on data IO and data persistence because the next step after building applications will be to connect to a database, he said. Within a few months, Pyscript will “have some really nice things out there” to support using the framework, he said.

The response from the Python community has been positive, Wang said, particularly from Python people who have seen the language struggle to be relevant in terms of building applications.

“They can see with Pyscript that now they have a way to just get stuff into people’s hands,” Wang said. “You find the people who really get what you’re trying to do, engage with them, and then keep building great things from there.”

Featured image via screenshot of Pyscript.net