Terminal Demo
Try the Python-Wasm Live Demos:
https://cowasm.org (uses Atomics and SharedArrayBuffers)
https://zython.org (uses Service Workers)
This is a demo of xterm.js and webpack with python-wasm.
Using python-wasm with webpack
You can use python-wasm
with webpack5. There are two things you may have to modify in your webpack configuration. See webpack.config.js, in particular:
The
NodePolyfillPlugin
is required becausepython-wasm
usesmemfs
, which requires several polyfilled libraries.The wasm and zip asset/resource rules are needed so python-wasm can import the python wasm binary and zip filesystem.
Thus your webpack.config.js
might include the following:
Synchronous IO
To use Atomic
and SharedArrayBuffer
for synchronous IO, your webserver must have the following two headers set:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
Your webpack.config.js
would then include:
This is optional: if you don't set the headers, then a service worker is used instead to support synchronous IO. No special setup is needed to use the service worker. If you do set these heϨaders, then other things on a complicated website may break since it's a highly restrictive security policy, e.g., GitHub pages does not support this.
Run the demo for yourself
Clone the repo:
Then visit the URL that it outputs, which is probably http://localhost:8080. You can then use Python. In addition the following should work:
control+c to interrupt running computations
input('foo')
for interactive inputimport time; time.sleep(3)
for sleep that doesn't just burn CPU
Do the following to force the fallback to service workers:
Note that the service worker approach causes a page refresh the very first time the page is loaded, so that the active service worker takes over proxying certain requests.
Supported Platforms: I've tested the above with node v14, v16 and v18 on Linux, MacOS, and Microsoft Windows. On Windows, you have to directly edit webpack.config.js to test out service workers.
Firefox and Service Workers
Unlike Safari and Chrome, Firefox doesn't allow service workers over http without setting dom.serviceWorkers.testing.enabled,
so set that to true in about:config
to test locally.