Run Python in the Browser with PyScript for Small UI Tasks
Use PyScript to run Python client-side for event-driven UI tasks, with caveats about main-thread blocking and package limits.
22 Jul 2025, 16:34 UTC

The problem: Python logic on a static page without a backend
You maintain a static site and need a small calculation to run when a user clicks a button—say, converting a temperature or formatting a value using logic your team already wrote in Python. Spinning up a backend endpoint for one interaction is overkill, but rewriting the logic in JavaScript duplicates code. PyScript offers a middle ground: it runs Python in the browser using Pyodide and WebAssembly, so the calculation executes on the client with no server required.
What loads when a PyScript page starts
The page loads a PyScript stylesheet and runtime script. The runtime brings in Pyodide, a CPython distribution compiled to WebAssembly. Once the runtime is ready, code inside <py-script> blocks runs in the browser. A <py-config> block can declare packages to fetch, but the exact keys and installable set vary by release, so check the documentation for the version you use.
Worked example: a temperature converter
Save the following as an HTML file. Load the PyScript assets for your release in the head, then add an input, a button, and an output container in the body. Place a py-config block and a py-script block after the DOM elements.
from pyscript import display, when
@when("click", selector="#convert")
def convert(_event):
celsius = 25 # read the input value using your release's Pyodide js interop
fahrenheit = 9 / 5 * celsius + 32
display(f"{fahrenheit:.1f} \u00b0F", target="result")
Expected check: open the file in a modern, Wasm-capable browser. Wait for the PyScript runtime to finish loading, then click the button. The converted value should appear inside the output element, and the browser console should show no PyScript errors.
Trade-offs and limits
- Main-thread blocking: Python execution runs on the browser's main thread. A long calculation freezes the page until it finishes. Keep PyScript work small and event-driven.
- Package limits: many PyPI packages fail under Pyodide, especially those with compiled C extensions, threads, or unsupported standard-library modules. Test any dependency in the target browser before relying on it.
- No security sandbox: Python code runs with the page's privileges, so treat it like any other client-side script.
- Startup size: the WebAssembly runtime is larger than equivalent JavaScript, so initial load is slower on slow connections.
How to decide and verify
Use PyScript when the task is small, runs in response to a user action, and reuses existing Python logic. Reach for vanilla JavaScript if the work must start instantly, or move it to a backend if it is long-running or needs packages that do not work under Pyodide.
Practical verification: open the page in DevTools, reload, and confirm the runtime ready message appears in the console. Click the button and confirm the output updates. Time the initial load against a plain JavaScript version to decide if the latency is acceptable for your users.
Verification note: PyScript markup, helper names, and config keys change between releases. Confirm the current syntax for your target version before using this example in production.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.