How to Create and Run Your Python Scripts in a Web Browser • The Register


Python, one of the world’s most popular programming languages, may soon become even more ubiquitous as it finds its way into web browsers.

Ethan Smith, a Berkeley-based software developer, recently revealed a project which allows CPython, the default implementation of the Python programming language, to run in web browsers through WebAssembly or WASM.

WASM is a binary format that provides near native performance in web browsers. It is a build target for languages ​​like C / C ++, C #, and Rust. It is commonly used to create performance sensitive code that JavaScript is not well suited to handling; marrying Python to WASM although its Emscripten compiler is more about ease of use and distribution than performance, at least at this point.

The project, created with the help of lead Python developer Christian Heimes, could make Python a viable alternative to JavaScript for some web applications and could alleviate one of the lingering issues in Python development: installation issues, dependency and management of the virtual environment are commemorated. in this XKCD comic that still torment inexperienced developers.

CPython on WASM follows a project that started in April called Pyodurus which also allows Python code to run in a web browser.

“The new project Christian Heimes and I are working on is aimed at making the web a supported platform for CPython, just like Windows or macOS,” Smith explained in a note to The register.

“Hopefully this will allow for a larger ecosystem of Python developers targeting the web and allow for easier integration with existing Python tools and processes, many of which Pyodide had to reinvent as micropip to replace the standard pip package installer. Finally, I would also like to look at WASI, the WebAssembly system interface, as a way to sandbox server-side Python code. “

Pyodid, Smith said, has a narrow focus – to run scientific computing code in the browser – and therefore uses a few hacks to get the wrapper to work properly. He sees the CPython project on WASM as having broader goals.

“I don’t want this project to be as specific as Pyodid, but we really want to work with them to make sure we don’t duplicate the work and that scientific computation workloads work well with CPython on WASM,” he said. -he explains.

“The main technical difference between our project and Pyodide is that we are not building a patched version of CPython,” he said. “Instead, we’re upstreaming our fixes, which have already made cross-compilation in general a lot easier.”

The WASM method

One of the limitations of WASM is that it does not support blocking / synchronous I / O operations, to accommodate JavaScript’s non-blocking / asynchronous event loop architecture. However, Smith says, “I intend to investigate the possibility of allowing synchronous I / O to yield to the browser event loop, which Pyodid does not currently do to my knowledge. This would allow easier integration with existing code. “

Those who want to run Python code in the browser will not immediately have access to the browser’s document object model, or DOM. Smith expects it to be possible, but its implementation will take time. DOM interaction will need to be done through JavaScript until WebAssembly implements a way to communicate directly with the DOM.

“I see that most of the early uses of CPython on WASM are Javascript calling Python to execute Python code, not the other way around,” he said. “There will always be places where Javascript does better than Python in the browser, but I think one of the best features of Python is its ability to act as simple paste code.”

Smith expects JavaScript to remain the preferred language for building user interfaces in web applications, but said he wouldn’t be surprised to see a Python-based front-end framework emerge that tries to take advantage of the CPython connection. on WASM.

He also said that CPython on WASM could be useful for developing cross-platform web-based applications, although he recognizes that work is already underway to bring CPython support to iOS and Android and that there are other cross-platform projects such as BeeWare.

When asked if the tie-up with WASM will make Python more competitive in terms of speed, Smith acknowledges that this could be a problem.

“Our first battle here is actually page load time, but we’ve already been able to reduce the size of the standard library to around 5% of its original size, and we can do more,” he said. -he explains.

“As for the speed at run time, I think the story will actually be the same as running on a server: if some of your code isn’t fast enough, call C, C ++ or Rust. ‘from Python shines. Rust in particular has excellent platform support for WASM and I think that would make a good pairing to speed up Python code running on WASM. “

CPython on WASM, he warned, is still in the early stages of development and the project is more ambitious than functional at the moment.

“If anyone wanted to start a project today, I would most likely recommend that they start with Pyodide, which has been around a lot longer and has more finishing,” he said. “But I look forward to working with the CPython and Pyodid developers to continue improving Python on WASM.” ®


Comments are closed.