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 .
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.
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.
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!
from pyscript import document
import time
counter = document.getElementById("counter")
i = 1
while True:
counter.innerHTML = str(i)
time.sleep(1)
i += 1
<script type="mpy" src="./main.py" worker></script>
https://chat-animator.com/
What does that look like in Python?
Final thoughts
PyScript is a platform .
It runs everywhere a browser runs.
Anything a browser can do, so can PyScript.
Write code and frameworks on PyScript.
It's open source... come play!
For the 99% (not just coders).