You loved running JavaScript in your web browser. Now, get ready for Python scripting

All thanks to CPython, WebAssembly, and some clever developers (And yes, there's Pyodide, too)


Python, one of the world's most popular programming languages, may soon become even more ubiquitous as it finds a home within web browsers.

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

WASM is a binary format that provides near-native performance within web browsers. It's a compilation target for languages like C/C++, C# and Rust. It's commonly used to create performance-sensitive code that JavaScript isn't well-suited to handle; wedding Python to WASM though 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 core Python developer Christian Heimes, could make Python a viable alternative to JavaScript for some web applications and could mitigate one of the lingering pain points of Python development: the installation, dependency, and virtual environment management woes memorialized in this XKCD comic that still bedevil inexperienced developers.

CPython on WASM follows in the wake of a project that debuted in April called Pyodide that similarly allows Python code to run within a web browser.

"The new project which Christian Heimes and I are working on has a goal of making the web a supported platform for CPython, just like Windows or macOS," explained Smith in a note to The Register.

"My hope is that this will enable a wider ecosystem of Python developers targeting the web, and allow for easier integration with existing Python tools and processes, many of which Pyodide has had to reinvent like micropip to replace the standard pip package installer. Eventually, I'd also like to look at WASI, the WebAssembly System Interface, as a means of sandboxing Python code server-side."

Pyodide, said Smith, has a narrow focus – running scientific computing code in the browser – and thus uses a few hacks to make packaging work properly. He sees the CPython on WASM project as having broader goals.

"I don't want this project to be quite as specific as Pyodide, but we definitely want to work with them to ensure we don't duplicate work and that scientific computing workloads work well with CPython on WASM," he explained.

"The main technical difference between our project and Pyodide is that we don't build a patched version of CPython," he said. "Instead we are upstreaming our patches, which have already made cross compiling in general much easier."

The WASM way

One of the limitations with WASM is that it doesn't support blocking/synchronous I/O operations, in order to accommodate JavaScript's non-blocking/asynchronous event loop architecture. However, Smith says, "I have plans to investigate allowing synchronous I/O to yield to the browser event loop, which as far as I know, Pyodide does not currently do. This would allow for easier integration with existing code."

Those wishing to run Python code in the browser will not immediately get access to the browser's Document Object Model, or DOM. Smith expects that's possible but will take time to implement. DOM interaction will have to be done via JavaScript until WebAssembly implements a way to talk directly to the DOM.

"I see most early usage of CPython on WASM being Javascript calling Python to execute Python code, not vice versa," he said. "There will always be places where Javascript will be a better fit than Python in the browser, but I think one of Python's best features is in its ability to act as simple to write glue code."

Smith expects JavaScript will remain the preferred language for creating 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 on WASM connection.

He also said CPython on WASM could be useful for web-based cross-platform app development, though he acknowledges there's already work being done to bring CPython support to iOS and Android and there are other cross-platform projects like BeeWare.

Asked whether the WASM tie-up will make Python more competitive in terms of speed, Smith acknowledges that could be an issue.

"Our first battle to fight here is actually page load times, but we've already been able to reduce the size of the standard library to about 5 per cent of its original size, and there is more we can do," he explained.

"As for speed at runtime, I think the story will actually be the same as running on a server: if some of your code isn't fast enough, call out to C, C++, or Rust. This is where the 'glue code' aspect of Python shines. Rust in particular has great platform support for WASM and I think it would make a good pairing for accelerating Python code running on WASM."

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

"If someone wanted to start a project today, I'd most likely recommend they start with Pyodide, which has been around much longer and has more polish," he said. "But I look forward to working with the CPython and Pyodide developers to keep improving Python on WASM." ®

Broader topics


Other stories you might like

  • Now that's wafer thin: Some manufacturers had less than five days of chip supplies, says Uncle Sam

    Components fabbed using 40nm-plus process nodes hit hard

    Hardware manufacturers hit hardest by the global semiconductor shortage had less than five days of chips in their inventories last year – and should expect supply chain issues to continue throughout 2022 – the US Department of Commerce said this week.

    Demand for semiconductors skyrocketed during the pandemic as folks purchased more PCs, laptops, and tablets to work or learn from home, and cloud giants scaled up their backend systems to cope. Supply, however, couldn't keep up. The median inventory of semiconductor buyers in 2019 was 40 days of supply. By 2021 that figure was down to less than five days for certain key US sectors, the department said in a report, while demand was up 17 per cent.

    Production was initially slowed at factories around the world due to shelter-at-home orders as the coronavirus pandemic took hold. Some facilities had to temporarily shut down after they were hit with natural disasters, such as fires and snowstorms. But between Q2 2020 and the end of 2021 fabs were operating at over 90 per cent capacity and still couldn't meet global demand.

    Continue reading
  • Baidu's AI predictions for 2022: Autonomous driving! Quantum computing! Space! Human-machine symbiosis!

    Did a computer program tell them to write this?

    Baidu Research's AI-centric "Top 10 Tech Trends in 2022" report has outlined the Middle Kingdom megacorp's predictions for technology over the coming year.

    Baidu CTO Haifeng Wang describes AI as a "key driving force of innovation and development," thanks to rapidly evolving core technologies, cross-domain connectivity, and expanding applications.

    It's no surprise that the list focuses on AI given Baidu's business domain. The Beijing-based company's search engine captures over 70 per cent of the Chinese market while also developing other products, particularly AI research and cloud computing. The research arm takes a deeper look at its associated technologies. Think Google but Chinese.

    Continue reading
  • Nvidia reportedly prepares for un-Arm'd fight with rivals: $40bn takeover may be abandoned

    Softbank, meanwhile, remains 'hopeful' it can offload Brit chip designer

    Nvidia is quietly preparing to give up on the purchase of Arm, according to Bloomberg, after repeatedly butting heads with competition regulators amid a wave of opposition from the tech industry.

    A report by the newswire states Nvidia privately told its partners it does not expect the Arm transaction to close. The report also claims Arm's current owner SoftBank is pressing ahead with an IPO of Arm.

    The $40bn bid Nvidia lodged for Arm in September 2020 has proved controversial: Arm licences its chip designs to multiple clients and some felt that buying the company will give Nvidia the power to stifle competition.

    Continue reading

Biting the hand that feeds IT © 1998–2022