Odds and Ends

Python in the Browser, 2 Ways

JupyterLite

WebAssembly (WASM) is changing the game with regards to Python running legitimately in the browser…

JupyterLite is a Jupyter Notebook/Lab and Ipython implementation based on Pyodide.

I caught wind of JupyterLite from this blog post.

Easily embed a console, a notebook, or a fully-fledged IDE on any web page.

In a previous blog post, we announced JupyterLite, a JupyterLab distribution that runs entirely in the web browser, backed by in-browser language kernels.

By default, JupyterLite ships with a Python kernel powered by Pyodide and IPython, bringing a wide variety of features from code completion to interactive visualizations.

The latest releases of JupyterLite now ship with a REPL application by default, that you can embed in any website.v

Pyodide

Pyodide is a Python distribution for the browser and Node.js based on WebAssembly.

What is Pyodide? Pyodide is a port of CPython to WebAssembly/Emscripten.

Pyodide makes it possible to install and run Python packages in the browser with micropip. Any pure Python package with a wheel available on PyPI is supported. Many packages with C extensions have also been ported for use with Pyodide. These include many general-purpose packages such as regex, pyyaml, lxml and scientific Python packages including numpy, pandas, scipy, matplotlib, and scikit-learn.

Pyodide comes with a robust Javascript ⟺ Python foreign function interface so that you can freely mix these two languages in your code with minimal friction. This includes full support for error handling (throw an error in one language, catch it in the other), async/await, and much more.

When used inside a browser, Python has full access to the Web APIs.

Sounds sweet. Below is the publically available in-browser JupyterLite REPL, but be sure to check out the super impressive demo of the full JupterLite Lab too.

PyScript

I recently learned about the Anaconda backed PyScript as well (from this blog post). Like JupyterLite, it is based on Pyodide and WASM.

PyScript is a framework that allows users to create rich Python applications in the browser using HTML’s interface and the power of Pyodide, WASM, and modern web technologies. The PyScript framework provides users at every experience level with access to an expressive, easy-to-learn programming language with countless applications.

What is PyScript? Well, here are some of the core components:

  • Python in the browser: Enable drop-in content, external file hosting, and application hosting without the reliance on server-side configuration

  • Python ecosystem: Run many popular packages of Python and the scientific stack (such as numpy, pandas, scikit-learn, and more)

  • Python with JavaScript: Bi-directional communication between Python and Javascript objects and namespaces

  • Environment management: Allow users to define what packages and files to include for the page code to run

  • Visual application development: Use readily available curated UI components, such as buttons, containers, text boxes, and more

  • Flexible framework: A flexible framework that can be leveraged to create and share new pluggable and extensible components directly in Python

All that to say… PyScript is just HTML, only a bit (okay, maybe a lot) more powerful, thanks to the rich and accessible ecosystem of Python libraries.

In short, our mission is to bring programming for the 99%.

All of this is going to be a big win for visualization, open science and reproducability on top of the obvious Python and documentation uses.

Tags: python jupyter webassembly pyodide jupyterlite pyscript html