[Music]
good afternoon or other time of day as
appropriate to your time zone everyone
um welcome back to kaya theatre for the
second half of saturday of linux
conference um
next up we have katie bell telling us
about how to run python in the browser
katie's 10 plus year career as a
software engineer has been pretty darn
fun you should read her full bio in the
program because it's pretty cool
fluent in several programming languages
she's at home both in both the web
browser and deep in the cloud she's been
teaching programming to beginners for a
long time and is now an instructor at
general assembly
after her talk katie will be taking
questions so please put any questions
you want me to read to katie at the end
if there's time in the questions tab
above the chat in venulis
and uh over to you katie
cool thank you so yes this talk is about
how to run python in the browser
and that title is actually a little bit
misleading because actually running
python in the browser is very easy so
this talk is going to be basically
structured as firstly an introduction
to why we want to do this and what
different approaches you can take we'll
spend two minutes showing you the entire
code for actually running python in the
browser
uh and then the rest of the talk is
gonna be about making really basic
things
like standard input and standard output
work in the browser so that's the vast
majority of this talk actually running
python in the browser is the easy part
but let's start off with a little bit of
why you would want to run python in the
browser uh to which i ask you why not
but possibly maybe you don't like
javascript i don't mind javascript i
write a lot of it
um but if someone doesn't want to learn
javascript and you would rather write
python then sure let's make that run in
the browser uh there might be some
python libraries that you want to use in
the browser or maybe
you have python code on the server and
you want to share it with the python
code in the client
or
possibly
you want someone to be able to write
python and run python without having to
install python on their own machine or
set it up or install the libraries like
you want it to be stupidly easy to like
get started running python by just
opening a browser window
and in particular that's useful for
something like a web ide
where you can just open up a browser tab
and have your full development
environment you could share code with
other people because it's some kind of
web service
and this is particularly useful for
education purposes where you might have
like a tiny web ide that lets you learn
python without having to go through the
process of like using like installing an
ide and setting everything up
uh so for my own purposes i've been
working on a project called split code
uh which is an in-browser ide with a
graphical uh structured programming
language and in order to be able to
write python code with this language and
run python code i needed to be able to
run python in the browser
so
to run python in the browser or have
some kind of python environment running
in the browser there are three sort of
categories of ways that you can approach
this problem
uh the first one running python on the
server we're not really going to talk
about much today because this is a talk
about running python in the browser
but if you see a kind of a python
interface or a way to run python code
through some kind of website like replit
or python anywhere uh it's not running
python in the browser it is running that
instance of python on some kind of
uh sandbox environment on a server
somewhere and it's connecting to it uh
via a websocket probably
and so you can have a beautiful you know
python environment with the python code
actually running on the server and not
in the uh browser and that works well
for some kind of cloud ide thing
um but that's not what i wanted to do
because i wanted the
python to be a lot more tightly
integrated within the browser
environment that i was using
and it didn't exactly serve my purposes
also if you provide that kind of service
you have to pay for all of the compute
power to run that python and it's
expensive to run the service if you want
to provide the service for free it's a
lot easier if people are running python
on their own machine in their own
browser
so
the second option is to instead of
sort of taking python and putting it in
the browser you build a version of
python that is specifically designed to
work with javascript uh in the browser
and it's not uncommon to have like
multiple versions of the python
interpreter so there are different
pipeline python implementations that are
designed to run in different contexts or
have different purposes uh see python
being the reference implementation for
python the language so
pi pi iron python jython micropython
they are other implementations that have
mostly started from scratch to implement
python
and these implementations take a lot of
time and energy to keep up to date
whenever the python language releases a
new part of the standard library or
whenever there's a new language feature
each one of these implementations has to
do the work to implement that feature so
they do tend to lag behind the latest
versions
of the python language
just because it takes a lot of effort to
maintain and some of these projects are
largely uh
behind now
uh there are three
implementations of python in javascript
already these are the three that i found
that are still actively maintained uh so
transcript brighton and sculpt
transcript is very much a tool designed
for
someone who wants to write python syntax
but really they're writing javascript
you don't really have a lot of the
python standard library you're expected
to just use javascript functions
javascript functions are just available
in the global namespace for you to use
and things like
using python style truthiness versus
javascript style truthiness on your if
statements is an optional flag that
increases the size of the javascript
that's generated from it so it takes
python generates javascript from it um
brython also takes python and sort of
transpiles it into javascript uh as well
but it has a lot more emphasis on a sort
of true implementation of python and a
full and complete implementation of
python it has a surprisingly
large proportion of the standard library
implemented um with obvious exceptions
for things that don't work well in a
browser
it even lets you import and use
pip packages that are
native python code that are pure python
code and not you know native c modules
uh so you can do a surprising amount of
python that is probably the one i would
use if you wanted to
have um a transpiled version of python
that
very closely matches uh what c python
would do in pretty much every
circumstance you could use it to uh
share code between the client and server
its primary purpose though is to replace
javascript for the language that you can
build web apps in you can use react with
bryton you can build uis it's focused on
uis manipulating the dom sort of
fulfilling that purpose of javascript uh
but with python instead
and it is kept up to date uh
and actively maintained and kept in line
with the latest versions of python which
is really cool
uh sculpt you'll find as well is still
maintained although they don't seem to
be putting a lot
of um
time into it uh it's still used it's
used by educational institutions mostly
and
uh its particular feature is that it has
like python total support so you can do
uh logo total graphics and stuff uh in
the browser using sculpt but it has a
bit of a different focus because it's
not designed to be a you know a full
python implementation
uh so for bryton uh the way that it
works is it takes your python code and
transpiles it into somewhat lengthy uh
javascript code um because it needs that
much work to be able to accurately
execute
python code in a way that is consistent
with the way that it would work in the
python language spec
so python is actually a pretty good
choice if you want to run python in the
browser
but it's not the one we're going to
focus on today uh because uh we're
focusing on the approach that i decided
to take uh which is compiling c python
so the reference implementation of
python
into webassembly and just straight up
running that in the browser
so to introduce this uh we need to talk
a little bit about web assembly and what
web assembly is
um a browser essentially supports
running two kinds of code it runs
javascript and it also runs
webassembly binaries or webassembly
instructions
as an example you can take other
languages in this case we have a
little c function that checks if a
number is a prime number or not
and what we can do is compile that c
function into webassembly and it might
look like this if you lay it out in
text form this is mostly for debugging
purposes you wouldn't really use this
text form you wouldn't write this text
form just helps you see
what instructions uh the webassembly
runtime has available to it
but more often
you would compile it into a binary form
or you can even include it as an array
of bytes
in your javascript code
and straight up run that so if you want
to play around with this and get an idea
of what uh webassembly is like to work
with
from a development perspective uh i
would highly recommend
uh wasm fiddle
uh wasn't fiddle so this is what i've
used to like write a little c function
which is the is prime function
uh you can build it
uh and it generates uh you can download
the binary file or you can see the code
here in binary or text format
and
uh you can and here is some javascript
code which
loads the webassembly module and
accesses the is prime function to run it
and call it uh so if i run this it tells
me one as a prime number oh sorry it's
not a prime number
uh 2 is prime
7 907 is prime
um and so we can build
a c function
compile it into webassembly and then
just call it
from javascript and it's running
in the same javascript
thread in the browser
so that's a fairly quick introduction to
what
webassembly is
but we want to use this
to run python in the browser
so what i originally thought i would
have to do uh would be to check out the
c python source code
and use a tool called m scriptin which
is a tool for compiling c code and other
languages that are llvm compatible uh
into webassembly uh you probably need to
patch some things or modify some things
to get it to fully work in emscripton
get it to compile and then you have
a webassembly binary that you can run in
the browser now this is a long process
unfortunately you can actually just use
one that someone else has made
so shout out to the piodide project
this originally started as just one
piece of the iodide project
and iodide is kind of jupyter notebooks
but for the browser a tool for doing
data science uh in the browser and as
part of that it needed to run python
iodide is now its own project was just
focused around python but it does have a
very strong focus on data science so
numpy and scipy and like common data
science libraries are ported to iodide
and are usable from pyodide as well
so the way that iodide works
it starts with the official c python
code applies a couple of patches add
some little uh utilities for interfacing
with javascript um and interfacing with
the browser environment a couple of
extra c files
it compiles it with
amscriptan uh and generates a web
assembly
and then has a javascript wrapper around
that webassembly that makes it very easy
to access and use uh which is really
cool
so this is how pyrodine works uh and so
if we wanted to summarize my entire talk
uh this is how to run python in the
browser uh this is the entire code that
you would need
most of this is just sort of html
boilerplate stuff so if we zoom in
um
iodide has a couple of javascript
functions to
load the piodide webassembly binary
and run python just by passing in
a string
so let's see that
working
go to the next demo uh so this is the pi
di test page this is exactly the same
code that i showed you in the slides i
copied it pasted it from the slides
rather than the other way around
um
but if i refresh this
it loads python
and it prints out the sort of version
info and then prints out the number 42.
uh if we go back to code we can see that
it does import this from the standard
library prints out the version and
prints out the result of 40 plus 2 which
is 42.
so we now have
with just a tiny bit of work
uh run python
in the browser done end of talk
uh we have now successfully run python
in the browser so that was the that was
the easy part
uh
we need to integrate this into the rest
of
javascript uh because we want a ui that
lets us write python run python and see
the output and and stuff like that from
python and this is
the hard part of the talk this uh code
for running python you can basically
pull straight from the piodide getting
started guide uh on the piodi
documentation
so actually getting running
python in the browser uh very very
simple
uh and it is a
like
straight port of c python uh it's not
its own thing
okay so building building a ui uh is a
bit trickier and we needed to have a
kind of terminal where we could see the
output uh and have it sort of printed
out
so i used uh xterm
which is the same terminal library that
vs code uses if you see a terminal in a
web page it's probably xterm js
then
so i have not shown all of the the work
that went into making the terminal
vaguely usable which was a surprising
amount of work uh but here we have when
we load piodide we can hook up
javascript functions for standard out
and standard error so that when
something in the python code writes to
standard out it will call this function
with the string that has been written to
stand it out or standard error
aside from that we just need a little
bit of javascript code that has a button
when the button is clicked it pulls the
code and runs it so this will make a bit
more sense if we can see
the ui
so we have a text area
and a terminal and so
the text area is where we write the code
when we click the run button it grabs
the code from the text area and throws
it into iodide so this code is
simplified in the slides but it's really
not that much more complicated than that
so i have a text box here i can write
whoops
print
hello world
i hit run and it says hello world sweet
we now have uh standard out from our
python running in the browser
uh if you want to try this and we have
things like
the standard libraries if we want if we
want to import random
we can
generate a bunch of random numbers using
the random library
if we want to use
some some of the other python libraries
like the ast module i can use the asd
module to pass some python code and
dump out the ast of that python code you
can write functions and run functions
and
all kinds of things it's a just
straight up c python port
if you try to do things like make
requests to
using sockets you might have a bit more
of a problem
um or things that are not
super supported by the browser
but for generally what you want to do it
just does work and
oops
just does work
uh it even has a file system which i can
show you so i have a little bit of
example code here where
we uh write to a file uh and then
read from that file again so uh
reasonably simple like it opens a file
it writes hello this is a file uh and
then it opens the file again to read
from it and prints out line by line
and if you're wondering
wait a minute browsers don't have access
to the file system uh you would be
absolutely right
the file system is kind of supplied as
part of
m scriptin when m scriptin uh sees that
you're accessing the file system in the
c code it will bundle in its own uh file
system implementation
uh under libsy
so emscripton during the compile process
goes oh python has access to the file
system has code that accesses the file
system uh it will sub in its own
in-memory file system there are options
in enscriptum to use like the
browser local storage or browser
indexeddb to store things in the file
system uh the only rule is it has to be
synchronous
uh it cannot sort of asynchronously
start writing to the file system and
then like wait for it in the javascript
land uh it has to synchronously write
and read from the file system
oops uh emscripten kind of subs out the
things that
your c code might normally get from the
operating system uh inscription supports
uh replacements for those depending on
what c libraries you're using so it
supplies this fake in-memory file system
uh it supplies overreads for uh standard
and standard out standard error uh it
has a sockets implementation you have to
turn on which uses a proxy server and a
web socket to kind of get past
the browser implementation i haven't
seen anyone actually using the sockets
implementation in anger
it has
bindings for sdl so you can use keyboard
and mouse input and joysticks and stuff
and you can draw to a canvas uh using
webgl uh through like the opengl uh
libraries and
so emscripton supplies like a lot of the
bindings that you would need to sort of
run applications that would normally be
interfacing with the operating system
for raw c code that does not interface
with the operating system uh it should
just work uh but the uh the ports are
needed for everything else
so we have two problems freezing the ui
and blocking on students so let me demo
those problems before we uh before we
start to fix them
uh if i write some code
which takes quite a while to run so here
is
just some python code that counts from 0
to 10 million
it doesn't print out too much because i
found that this was actually the like
printing to the the terminal was the the
slow part of this um but if i run this
code
it just kind of freezes the ui for a
couple of seconds like if we see that
again like normally i can select this
text i could edit the text i can use
these buttons but while the code is
running the buttons are unresponsive the
text is unresponsive
if you accidentally do a
like infinite loop you'll basically lock
up the tab permanently and you'll need
to get
chrome to kill it
and you'll also notice
that while it's running it's not
actually printing to stand it out
until it gets to the end and that's not
because it's not trying to print a
standard out and it's not because the
standard out isn't working it's because
it is locking up the main browser thread
while it's processing and so
the browser thread cannot update the dom
and cannot update the ui until it's
finished
so this is obviously a little bit less
than ideal
for running python code in the browser
you don't want your whole ui to lock up
just because your python code is taking
a while
and the same time if we want to do
something with standard input
by default standard input is implemented
like this
uh it uses the browser prompt
and brings up the prompt so if you
wanted to say it's saying enter your
name so i enter my name
um and then it can continue on with the
program
the reason it uses a prompt uh is
because
the prompt will pause the thread while
it waits for the result of the prompt
the prompt is synchronous
and it blocks the whole browser thread
while waiting for input
uh
it's
non
like you know you can't have
a browser ui that accepts input while
the main thread is blocked in in any
other way
because the ui needs to be working for
it to accept input
so we have these two problems
uh one is whenever we're running python
code it freezes the ui and two uh we
can't like block and wait for standard
in
input when we're running the python
program
uh so we need to solve both of these so
normally when you're running a regular
python application
and
uh and we have a little
demo here
um
oops i need to get
out of full screen for this to work
um
whoops
so if i do
uh
a read from standard in in python
then that python process is now blocked
the python thread is is blocked until
i
until i finish entering the input and
then it continues on
so
the way that python works in terms of
making a blocking system call just
doesn't really work in the browser land
especially not on the main thread
because it will block up your whole ui
uh so there's two ways to make python
execution stop and wait
uh emscripton has a tool called asyncify
which
allows you to add into the c code hooks
to let it stop and and wait
um it does this by kind of unwinding the
whole like
call stack and then when it resumes it
will like reload the call stack it has a
fairly big performance overhead even
when you're not
actively using it and and stopping and
waiting
um but option two is the one we're going
to look at which is having a separate
thread a separate process uh which does
blocking
and in order to do that we need to use
three different
browser features that you might not have
used before even if you do
a fair amount of web development
and that is web workers a shared array
buffer and the atomics
library
so if we have a look at these a web
worker
is a way in javascript uh or in
webassembly in the future to load
a
separate thread and a separate
javascript execution context so
the main thread which is where your
javascript is normally running can call
new worker and specify the javascript
file that it should use for that new
worker and it starts a separate thread
which is executing javascript and the
main thread and the worker thread can
communicate with each other with this
post message
event that they can send each other
which they can listen for asynchronously
and at any time the main thread can
terminate the worker thread so this is
really useful if you want to say have
some heavy number crunching that if you
were to run it in the main thread that
heavy number crunching would
lock up the main thread and cause your
ui to freeze uh you could instead run
that in a worker
and have it send the result back when
it's done
so this worker thread doesn't have any
access to the ui to the document
but it is there to do sort of background
processing without locking up the main
thread
which is super useful
for us
as we do
python because we can run our python in
that worker thread
or in a worker thread you can have many
workers
so the code for this uh simplified again
uh is in the main thread we create a
worker which is a separate javascript
file
uh and we wait for messages from it uh
and we're assuming that the messages
come from it that come from it are
strings that are our standard out and
we'll just put them straight to the
terminal
and when we want to run code we can say
worker.postmessage and send it the code
as a string as well
and on the worker side it loads our
python context
and loads our python web assembly and
when it receives some code it runs the
code and when it has standard out uh
strings then it will post those back to
the main thread so the main thread is
sending the code to the worker and the
worker is sending the standard output
back to the main thread
and so if i bring another terminal up
we can do the same code that we were
running before that was just a fairly
busy loop that takes a while
and when we run it our ui keeps working
and it can output and update the ui with
the standard output as it goes because
this python code is running in a
separate thread and it is not blocking
or tying up the the main worker thread
so if we run that again
oh look it's so beautiful
um it's printing out all kinds of stuff
which is really helpful
uh it also means that because our ui
isn't locked up i haven't implemented it
here but you can have a kill button that
you know if you accidentally write an
infinite loop in your python you can
still have a ui with a button where you
can click the button and it will
terminate the worker process uh so you
don't have to worry about an infinite
loop kind of completely crashing uh the
browser tab
uh
but this doesn't help us with
standard in because in order for
standard in to work we need to block
that worker thread
and so we do this with the other two
browser features that we need
uh which is the shared array buffer and
the atomics library
and so
with these two things
we can set up a system which lets our
worker process wait for the main thread
and the ui to capture some standard in
and send it back
so we start off with the main thread
launching the worker thread
like we did before
but this time our main thread can send a
post message to the worker thread with a
shared array buffer now this is
simplified code that doesn't actually
work but it's sort of pseudo code in
javascript like pseudocode
our main thread and our worker thread
now both have a reference to the same
uh block of memory right that shared
array buffer is memory that can be
shared between the main thread and the
worker thread so it's under the buff
variable and the main thread it's under
the buffer variable in the worker thread
but they are using the same
bit of memory inside that shared array
buffer
and if in the worker thread we call
atomics.wait using that buffer
that worker thread is now blocked it
will wait
for that if there's other javascript
that's running or other
uh webassembly that's running all of
that will just wait
uh until something else happens uh this
worker thread is now not using the cpu
it's not a busy loop or anything it is
just blocked
and in this case it's blocking on
that
buffer at index zero and blocking
waiting until it is no longer uh zero no
longer has a zero value
so on the main thread we can
modify that memory we can store the
number 42
in that uh that
index but when we call atomics.notify
on that buffer
that
finishes this weight operation it
unlocks unblocks this worker thread and
this worker thread can now continue on
with the next line of code or the next
thing that it was about to do
so the worker thread waits for the main
thread to
finish for the user to like type in some
input or something and then uh and all
of that stuff can happen asynchronously
and the worker thread will block until
the main thread calls notify
and so using this
we can implement
standard in
so if we use this example
and i bring back the example code i had
so again super simple code it uses input
to prompt the user to enter the name
waits for that input uh and then prints
the name
so if we run this uh the worker thread
starts processing the python code um our
python code is now blocked it's now
at that atomic.weight point and it is
waiting for the main thread to call
notify for it to wake up again so
uh my ui still works uh i can enter my
name here
and when i finish entering the standard
input and when i hit enter that's when
it's the main thread is going to call
atomics.notify
and then my python code can continue to
execute with that standard input uh
coming in
so very simple program just being able
to enter your name and have it come back
to you uh using standard in and stand it
out
was using a lot of browser features and
a surprising amount of work uh but uh it
does work uh which is cool
and because we now have actually working
standard in and standard out uh it means
we can use uh a python
repo using the actual python ripple if
you find
a um the piodide website has a a repel
example on it but the
uh ui the rebel itself is implemented in
javascript and it just sort of sends one
line at a time of python code into the
into the um
into actual python so but this is a
python interpreter
actually running in python um just
reading from standard in and printing to
stand it up
so
you know we can you can do all kinds of
things
input
something like that
so
uh yeah we have a working python repo uh
and if you it is it is live on the
internet and i have the link uh in the
slides later on if you want to try that
out yourself
uh so
we now have working standard in uh and
standard out and a full working repo
uh in the python in python using c
python uh and sort of working seamlessly
with the terminal in the ui
so we needed a web worker to run python
in a separate thread
away from the main
ui thread
we have a shared buffer to share memory
between them uh and atomics to kind of
block and wait for that shared buffer to
be filled with the standard input
okay
so as a side note browser support for
these features for shared buffer array
shadow ray buffer
and atomics in particular
is a little bit patchy and that's
largely because
the browser started supporting it and
then when spectre happens they realized
that this was actually a very big
security concern
and browsers removed support for the
shared array buffer
and atomics was generally still there um
but is not very useful without the
shared array buffer
and so
uh bit by bit browsers have been
re-adding support for shared array
buffer
but with a bit more security uh
implemented in it
so in particular in order to use shared
array buffer the page must be running in
a cross-origin isolated context and this
is mostly to stop uh something like oh i
embed some ads in an iframe but that
iframe can use shared array buffer to
spy on the rest of the process
and spy on what's happening in the main
the main thread
so you can only use shadowraybuffer in
context where
you can guarantee everything is coming
from the same origin or at least was
specifically allowed
uh so
um
the support across browsers now once you
have added these headers for your site
uh is is pretty good uh edge firefox
chrome they'll support it safari only
just recently re-added support for it a
couple of weeks ago so if you're on the
latest version of safari uh then you'll
have it uh it's pretty good on mobile as
well
uh now but if you look at atomics
uh the atomics library which is very
necessary for this whole approach um
doesn't look like it's very well
supported uh at all
uh this actually is because the can i
use data is out of date
um it hasn't been updated for
uh like the latest safari did add
support for it but it's not there
so i've been going through trying to
find all of the browsers uh that do
support it even though the mdn data
doesn't say that it's supported and it
is actually supported across more
browsers uh than you would think and i'm
working on
i have one pr sent already to update it
uh and i need to work on a couple more
to trace down
exactly what versions it was added in so
the browser support for shader ray
buffer and atomics is actually better
than it looks
uh at the moment um and it has pretty
wide support
uh
looking forward um i am working i want
to work on more
python library support uh in
uh in the browser
i want to be able to do things like
uh graphics drawing to a canvas um
making sure all of the audio stuff works
a lot of that stuff will already work uh
thanks to emscripton's
libraries but making that easier making
sure it all works
um as well as supporting making network
requests uh which is gonna be an
interesting security question um
there is
progress in
underway to add support for threads and
atomics uh into webassembly itself so
you don't need to sort of drop out into
javascript to be able to do those things
you generally don't need to do that now
um but it's not
this uh threads in atomics is not an
official part of
uh the webassembly standard yet even
though a lot of browsers do support it
um
there is another project which i only
found out about recently uh where a
couple of c python core developers are
working on building support for
compiling c python to webassembly oops
without any patches
there's also a proposal to add
atomics.weight async which would allow
the main thread to sort of
asynchronously wait on a shared array
buffer which would remove some of the
necessity of
uh using post message to
send standard out back from the main
thread you'd be able to have the ui
update without the overhead of using a
post message and serializing everything
um
and there's also some really interesting
work going on for using web assembly for
sort of quote unquote native apps
running webassembly outside of the
browser with access to the operating
system uh which has a really cool
potential for sort of native-like speeds
but allowing the same binary code to be
portable across uh architectures uh
which would be really cool
so
in building this a lot of credit goes to
ben taylor uh who helped a lot with
getting the terminal ui set up because
he also has his own project uh and greg
dark was super helpful explaining things
like uh blocking system calls to me
um
ben has his own project which you should
absolutely check out are called runner
runner.dev
it is an embeddable editor and execution
environment that you can include in any
website just by including an iframe
and that also uses webassembly to run
python
but it is not an enscripted version of
python it's a different version of
python and is built using
uh wazi instead of inscriptin okay
so yeah that's my talk uh and i do
believe we have some time for questions
uh i can't hear you betsy i don't know
if everyone else can
that's because i had myself muted
it was bound to happen
um
thanks for telling me katie or i would
have kept going um yes we have plenty of
time for questions and we have a few
questions there um so thanks again katie
for that talk um that was just as fun as
i was hoping it would be um
i love seeing things run in places where
they were never designed to be run um
and speaking of
um our top question is
can inscription itself run in the
browser
oh
that's a good question i don't think
i've ever really looked into like how
inscription itself
is run um
if it takes
itself written in c then maybe
it's not really doing much except sort
of reading files and doing a lot of
processing so i imagine it would but i
have never tried
i don't even know if it's written in c
something for the asco to play with
uh how large is the web assembly
executable of python executable in
quotes oh that's a great question
because the pioneer team has put a lot
of work into this uh it's surprisingly
small it does take a noticeable amount
of time time to download um but it's
something like
like the whole download would be
in the order of six or seven megabytes
so it is less stuff to download than
loading the new york times home page
and it's like you can sort of see it if
i open it up
and we open the inspector
and the network tab
alright let's have a look
uh the whole thing is 7.4 megabytes
of transfer
stuff so
a lot of that would be
um
here
in
the piodide data and webassembly which
is 3.4 megabytes and 3.6 megabytes
makes sense could be a lot more
it could be it was a lot more um but
it's gotten better
that's great
okay next question if standard n is
blocking execution of the worker thread
can you launch another worker thread to
allow continuing execution
uh yeah you can launch as many worker
threads as you want to run
separate uh python
uh they'd be sort of separate python
i mean i'm gonna say processors sort of
they're sort of they wouldn't be able
they wouldn't be sharing their variables
between them
they wouldn't be sharing memory between
them but yeah you could run two copies
of python uh in separate worker threads
uh just fine
that makes sense
how easy would it be to get
non-supported libraries running
so piodide already has support for
uh libraries that are written in pure
python so you can use
pip packages if the pit packages are
implemented
in pure python
it will there are some python libraries
that uh iodide has specifically built
support for uh things like numpy uh that
have a lot of native code as well but
you would have to separately compile
any native code into webassembly for it
to work
um i've not gone through that process
myself um it probably depends
on
uh how easy or hard it like what what
the library does and how easy or hard it
is to to why them if
um but if it is just doing something
like calculations and not relying on the
operating system then i imagine it's
pretty doable um that being said i have
not attempted to do that myself
i'm very glad to hear that they've
already sorted out numpy because that
would be the worst to try yourself yes
um have you got test results on what
performance hit the webassembly
translation
than execution causes compared with
native python
um i haven't done any benchmarking
myself uh the piodide project quoted
something like
uh
two to three times
uh as like
slower than c python um so it is like
aries a noticeable performance hit
compared to c python
um next up why do you need a shared
buffer to send standard and data rather
than using the post message function
again uh the big problem is that
the post message
uh
needs to be asynchronously received by
the worker
and so
once you call
like while it can't asynchronously
receive that standard input
until the worker thread is unblocked
um it can't kind of store it and then
uh
start using it as soon as it it as soon
as the wait is over um so
it has to wait for the standard input
and then get it synchronously as well um
but if you tried to do that by sending a
post message
it wouldn't receive the post message
asynchronous
like event until after it had finished
requesting the standard in synchronously
i'm not sure i explained that very well
but it sounds like something that needs
like diagrams yeah
maybe i should have added a diagram for
it yeah
um that call to like read standard in
from webassembly
can't do anything asynchronously yeah
yeah
okay um do you know whether any bigger
sites use existing systems like brythen
and production
uh i don't know of any bigger sites that
do it if you go to the bryton website
they do have a list of
projects that use brighton um some of
which are sort of just regular web apps
um i didn't notice any big names not
from memory
um
for piodie uh sorry it has a list of
projects that use brighton a lot of them
are you know run python in the browser
educational tools
um for teaching python uh in the same
way um
the piodide site lists projects that use
piodide and most of them are data
science
tools um
so like that sort of follow the jupiter
notebook style interface where you have
chunks of python code uh that you can
execute
that makes a lot of sense
okay we are just about out of time um
there are there's one more question
which i'll leave for the chat um in the
post conference post talk chat i'm
already not able to say
the kaya theater post talk text chat um
i'll paste that one in
and also quite a lot of people in the
chat and the questions are asking for a
copy of your slides katie um so just so
you know
but thank you very much katie uh that
was
a really interesting look into the
mechanics of running things in the
browser
um and
that's it for now
back in 10 minutes
thank you
you