MicroPython Technical Preview

PyScript Runtimes - MicroPython Technical Preview

PyScript is an open-source platform, using web assembly (WASM), for Python in the browser.

Given this HTML fragment:

<py-script>
def hello(name="world"):
    """
    Return a friendly greeting.
    """
    return f"Hello, {name} (from PyScript)"

print(hello())
</py-script>
<script src="pyscript.js" type="module"></script>

You get:

def hello(name="world"): """ Return a friendly greeting. """ return f"Hello, {name} (from PyScript)" print(hello())

Why not "view-source" and see for yourself?

What?

The initial version of PyScript, demoed in a keynote address by Peter Wang at PyCon 2022, built on the amazing work of the Pyodide project. But Pyodide isn't the only Python runtime, compiled to WASM, that could be used for this purpose.

These pages are a technical demonstration, proof of concept and playful exploration of how PyScript may work with other scripting language runtimes. This initial work focused on the amazing work of the MicroPython project (a full re-implementation of Python 3 targeting constrained computing environments).

Here's what Damien George (creator of MicroPython) says about such efforts:

MicroPython was written from day one to be easy to embed, and tries to support a wide variety of targets. For example, in a minimal configuration, the only connection to the external world that it needs is a place to output characters.

Although I never had the idea that MicroPython would be embedded in the browser, it actually fits quite naturally because MicroPython is very self contained. MicroPython's small size and efficient use of resources (RAM and CPU) means that it's well suited to being embedded in a webpage, because the download size and start-up time is minimised. It's pretty exciting to think that MicroPython could gain traction as a way to build websites.

Why?

Different runtimes exhibit different strengths and weaknesses. As always, there are tradeoffs.

Whereas Pyodide is a relatively mature version of standard CPython, including powerful native modules such as numpy and scipy, it's also rather large (circa 11Mb) and slow to start up.

Alternatively, MicroPython is a highly regarded, mature and battle tested reimplementation of Python 3 but with "micro" reimplementations of popular libraries. It is small (base 303Kb) and startup time is only a few thousandths of a second (from a warm cache), even on mobile devices.

Given such tradeoffs, we want folks to be able to choose the runtime to use with PyScript that best suits their unique requirements.

How?

The code on these pages is experimental.

A small, fast and simple implementation of PyScript, created for testing purposes, allows us to quickly explore the characteristics of different potential runtimes.

This version of PyScript, simply does four things:

  1. Read custom configuration, otherwise fall back to sensible defaults.
  2. Download and start the language runtime. Currently, this is configured to be MicroPython.
  3. Register and manage plugins that contain the PyScript platform's logic. Currently, there are two: one for the <py-script> tag, and one for the <py-repl> tag.
  4. Load user defined files onto the runtime's virtual file system (data, code, modules etc).

Issues

This is experimental and not intended for production. Please report issues here.