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.
â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.
Thanks to recent commitments from @ChristianHeimes, I found the correct incantations to create the main branch of CPython for the web!
This is the original Python that runs * in the browser *.
My scripts to build this are at https://t.co/UxhHLBupj4
A lot of work to sort things out though … ð· pic.twitter.com/gIFlVNpNiX
– ethantyping (@ethanhs) November 26, 2021
â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
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.” Â®