Python in the Browser - Run! Run! Run!

Praveen Sridhar (~psbots)




How many times have you banged your head on the wall while using Javascript to build a page showcasing your shiny new Python project?

Wouldn't it be great if your blog readers could run and play with the code right away?

Fancy running Jupyter-like notebooks entirely in the browser without any server?

This talk will get you a headstart into running Python directly in the browser.


Introduction (2 mins)

  • About me
  • Why the Browser is an important stack to target?

Three major approaches

We will be peeking at the different approaches that have been taken to target Python on the browser and compare and contrast them by digging deeper into how they work under the hood. Brief details below :

  • Transpilation (6 mins) - Python code is converted to Javascript before the page is loaded.[HTML_REMOVED] This has the advantage of having pure JS as a final output, without any need for a large file to be loaded on the browser. However, the implementations are not feature complete with the Python standard and introduce different ways to handle JS quirks. [HTML_REMOVED] Examples include PyScript and Transcrypt[HTML_REMOVED]

  • Python implementation in Javascript (6 mins) - Python code conversion to JS takes place in the browser itself. [HTML_REMOVED] This approach creates full blown Python implementations in JavaScript such that all Python code is executed realtime in the browser itself. The disadvantage being that the VM needs to be loaded into the browser. One such project, Skulpt was very successful in using this approach as a teaching tool, where students could run their scripts directly in the browser based online course. Another project that uses this approach, Batavia takes in Python bytecode as generated from cpython and interprets that in the browser realtime using a Javascript implementation of the Python VM. [HTML_REMOVED] Examples include Brython, Skulpt and Batavia[HTML_REMOVED]

  • Web Assembly (6 mins) - Converting full implementations of Python to run on the web [HTML_REMOVED] This approach involves compiling entire Python implemetations like PyPy or CPython to JavaScript using Web Assembly. This is the best approach to go with if aiming for full Python feature compatibility, but has a major disadvantage as the size of the compiled implementation is quite large. [HTML_REMOVED] Examples include PyPyJS and Pyodide[HTML_REMOVED]

Conclusion (5 mins)

  • Is there a single best way to go about this problem?
  • Learnings about Python internals
  • This area is still in its infancy - what to look forward to?


General overview of how Python works under the hood - What happens when you run a Python file using CPython, what Python bytecode is etc.

Speaker Info:

Currently working as a Freelance Python Developer based in Kochi. Originally did Bachelors in Mechanical Engineering from CUSAT. I have completed consulting projects in ML and AI with multiple startups and companies. My work on CNNs was the winning solution for IBM’s Cognitive Cup challenge in 2016 and gave a talk on the same at the Super Computing conference SC16 at Salt Lake City, Utah : Slides

Previously I was a Technology Innovation Fellow with Kerala Startup Mission where I started a non-profit student community TinkerHub, that has a focus on creating community spaces across colleges for learning the latest technologies.

I've been dabbling around with browser technologies since my college days since 2011 being a Mozilla volunteer which got me interested in finding ways to run Python in the browser.

Id: 939
Section: Core python and Standard library
Type: Talks
Target Audience: Intermediate
Last Updated: