Classically trained former professional orchestral
musician.
Teacher (mostly UK inner-city teenagers), but also
kindergarten to adult education seniors (and everything
inbetween).
Writer (The Guardian, several O'Reilly titles, and other
magazine related stuff).
Academic background in Philosophy.
Principal engineer on the PyScript team at Anaconda.
Explain bunnies and Anaconda's relationship to PyScript.
What we'll cover about PyScript:
How exactly does it work?
What exactly do you get?
What exactly can you do?
So, what is PyScript?
A platform for Python in the
browser.
Sits on top of Python interpreters
compiled to web assembly (e.g. Pyodide, MicroPython
etc).
Different interpreters have
different strengths and weaknesses.
PyScript makes it simple and easy to
create and use frameworks or your own application code on
top of the platform.
That's it!
Question:
You want to share this Python app with Grandma.
print("Hello, world!")
How?
๐ฌ๐ฒ๐ค๐ฑ
Grandma is clearly a proxy for someone non-technical. Let's
just consider all the skeletons in Python's cupboard for
getting our work to end users.
Install Python and then pip install blah... not a good
look.
Shout out to BeeWare trying to work within the walled gardens
of native apps.
PyScript is...
...A platform for Python
in the browser .
On your desktop
On your mobile or tablet
...and any other appliance with a built-in browser!
Emscripten allows you to port existing C/C++ code to WASM on
a web platform.
It's like the libc / POSIX / win32 layer, that abstracts
away the browser to provide common concepts like a
filesystem.
So how do you create an environment in which to run
Python?
Except, packaging with PyScript is
mostly like Python:
packages with C extensions need to be compiled to
WASM.
you have to install all the packages each time you
navigate to a URL.
sometimes, the browser isn't friendly to popular packages
(e.g. requests).
Work is ongoing to solve these puzzles.
from pyscript import window
from pyscript import document
<button id="click-me">Click me! ๐ญ</button>
from js import document
def handler(e):
"""
It's just Python! Access the DOM!
"""
output = document.createElement("span")
output.innerHTML = "๐ฑ๏ธ"
document.body.appendChild(output)
button = document.querySelector("button#click-me")
button.addEventListener("click", handler)
Clicky Mouse-o-Matic ๐
https://tinyurl.com/yc4hjz33
Given we have access to JavaScript's globalThis, we have
full access to the browser's APIs.
<p><button micropython-click="camera_click">
Start Camera ๐ฅ
</button></p>
<video id="video" width="600" height="400" autoplay />
import js
async def camera_click(e):
media = js.Object.new()
media.audio = False
media.video = True
stream = await js.navigator.mediaDevices.getUserMedia(
media
)
video = js.document.querySelector("#video")
video.srcObject = stream
Lights ๐ก Cameras ๐ฅ Action ๐
https://tinyurl.com/4dtpmrcw
This example is based upon some work by a buddy of mine,
Professor Chris Rogers of Tufts University.
This is PyScript interacting with a Lego Spike Prime device
that is itself running MicroPython.
The PyScript app uses the browser's USB serial API to connect
to MicroPython running on the Lego device.
The device itself, is an Easter Egg roller, for decorating
eggs.
Egg roller (needs LEGO robots) ๐ฅ
https://tinyurl.com/4cc4bd2x
Tufts' Prof.Ethan recently demoed a version of this at the
White House.
Talking of things that spin! There's more..!
Ever seen this on a web page?
We're blocking the UI because browsers usually use a single
"MAIN" thread for all JavaScript on a web page.
It's the same with PyScript.
Please don't block the main thread!
https://chat-animator.com/
There's a Xworker proxy between the MAIN thread and the
WORKER.
Transparent message passing done in a way that will
pause the worker (allowing synchronous Python code) while
the MAIN thread remains unblocked.
Achieved via a web standard for sharing memory between
workers and the main thread, called Atomics.
What does that look like in Python?
Main thread ๐งต
from xworker import XWorker
for i in range(12):
sync = XWorker(
"pompom.py", config="turtle.toml", type="micropython"
)
Worker code (1) ๐ข
turtle.speed(8)
turtle.pensize(12)
for i in range(100):
turtle.penup()
turtle.setpos(0, 0)
turtle.left(random.randint(1, 360))
turtle.pendown()
turtle.color(random.choice(colours))
turtle.forward(random.randint(20, 90))
Worker code (2) ๐ฌ
turtle.Screen().show_scene()
result = turtle.svg()
from xworker import xworker
document = xworker.window.document
container = document.createElement("span")
document.body.appendChild(container)
container.innerHTML = result
This is a video of the code we just examined.
I don't want my laptop to melt.
You get the idea...
Turtles on workers ๐ข๐ข๐ข๐ฅ๐ฑ
https://tinyurl.com/5n8zjj9p
(Chrome only - may melt your laptop.)
It's very early days and we're only just scratching the
surface of PyScript's potential, both for the Python world and
the world of the browser.
Final thoughts
PyScript is a platform .
It runs everywhere a browser runs.
Write code and frameworks on PyScript.
It's open source... come play!
For the 99% (not just coders).