python-wasm
python-wasm copied to clipboard
Demo executable examples in docs
If we can get code examples on docs.python.org to be executable in the browser then that might be quite the boon for this project, motivation for others to get involved, and a way to make sure things don't break. 😉
I love this idea. Relevant Twitter thread: https://twitter.com/alsweigart/status/1480623244919156741
I don't think loading Python WASM for every user who visits a documentation page is a good use of resources - either for the PSF servers or the user's browser. So instead I suggest this works by loading on-demand.
A code example might be presented as a regular syntax highlighted block of code on the page, with a "try this" button.
When clicked, that button would load the relevant Python WASM version and replace the static code block with a pre-populated interactive interpreter.
Thanks to browser caching, any subsequent clicks of that button on other pages should respond really quickly.
Beyond some Python docs infrastructure, hooking up a web REPL to the WASM Python build, which accepts query parameters for lines of code to run ahead of time (and possibly lines of code to run invisibly to set up an environment) seems like the blocker. This should probably run in a sandboxed or separate-domain iframe to prevent user code from running arbitrary JavaScript on the Python docs page.
Some REPL like the one in the nested iframe on the front page of python.org provided by PythonAnywhere (for design inspiration, implementation may differ since that uses a websocket to a remote server) or the Pyodide REPL (see https://github.com/ethanhs/python-wasm/issues/32).