python-in-browser
Running Python in the browser using Batavia and Starlette.
Demo
# frontend/app.py
import dom
print("Hello from Python!")
dom.document.title = "Hello world"
div = dom.document.getElementById("pyconsole")
div.innerHTML = div.innerHTML + "\n\nHello, World!\n\n"
raise ValueError("Just testing out exceptions!")
Installation
First, install Batavia. The full instructions are here, but we provide a convenience script that should work on Linux/macOS:
chmod +x scripts/install_batavia
./scripts/install_batavia
Then install Python dependencies:
python -m venv venv
. venv/bin/activate
pip install -r requirements.txt
You're good to go!
Quickstart
Start the web server:
uvicorn server:app
Go to http://127.0.0.1:8000, and the following will happen:
- An HTML page is shown. It was server-rendered via Jinja2 by the Starlette web app.
- The HTML page contains a script that contains the Python bytecode for
frontend/app.py
. - The Batavia JS virtual machine is loaded and runs that bytecode.
- As a result,
frontend/app.py
ends up running in your browser, and doing stuff such as printing to the browser console and manipulating the DOM.