PyScript and pyscope: Running Python Interactively in the Browser
A practical look at using PyScript for browser-based Python including a worked example of pyscope interop and key limitations to consider before committing
16 Jul 2025, 10:50 UTC

The Problem: Python in the Browser Without a Server
Web pages typically run JavaScript. If you want to execute Python code perhaps for a data analysis widget a calculator or a learning tool you usually send data to a backend PyScript changes that by embedding a Python runtime directly in HTML
How PyScript Works
The Pyodide Runtime
PyScript relies on Pyodide a Python to WebAssembly compiler that brings a curated subset of the Python standard library and many third party packages into the browser When the browser loads a py script tag Pyodide downloads compiles and starts up before your code runs This start up can take several seconds on slow connections which is the first trade off to consider
Two Way Interop With pyscope
The most distinctive feature in PyScripts interop layer is the pyscope object It lets Python code invoke JavaScript functions through a simple call syntax
import numpy as np data = np.array([1, 2, 3, 4, 5]) pyscope.call('displayResult', data.mean())On the JavaScript side define a function with the given name that receives the value and updates the page as needed
function displayResult(value) { // This function is called by PyScript update the DOM as needed }A Worked Example Browser Based Statistics
Suppose you want a page where a statistical result is computed entirely in Python without a network request The Python code below uses NumPy to compute the mean and standard deviation of a small array then passes both values to JavaScript via pyscope call
import numpy as np data = np.array([10, 20, 30, 40, 50]) pyscope.call('showStats', data.mean(), data.std())Define the JavaScript receiver function to display the results
function showStats(mean, std) { // This function receives the two values from PyScript }When the page loads Pyodide downloads the Python block executes and the showStats function is called You can open the file in a browser and verify the values appear in the console or a designated element
Trade Offs and Limitations
Start Up Latency
Because PyScript must load Pyodide a multi megabyte WebAssembly module before any Python runs initial page load can be slower than a pure JavaScript equivalent On fast connections this is often imperceptible for a single script but repeated loads or slow networks will notice the delay
Standard Library and Package Constraints
The Python library available in PyScript is exactly what Pyodide packages Pure C extensions modules that rely on system level headers and some rarely used standard library modules are unavailable If your code imports csv or json those work because Pyodide ships them but a package like pandas may require an extra download depending on the PyScript version
Security and File System Access
Browser security policies restrict direct file system access Reading a local file from a file picker into Python requires explicit permission and writing back to the client filesystem is generally not allowed without a user gesture and the pyfilesystem2 shim which PyScript may or may not include depending on the build
Verifying Your Setup
- Create a new HTML file and add a single py script tag with print Hello Open it in a browser Hello should appear in the DOM or console confirming the runtime loads
- Try importing numpy add import numpy as np arr = np.array([1, 2, 3]) pyscope call log arr sum and define a log JavaScript function that console logs the result Use the browsers developer tools to confirm the import succeeds and the call executes
These steps match the verification hints from the PyScript documentation and should take under a minute on a typical connection
Closing Is PyScript Your Tool?
PyScript is compelling when you need Python semantics in a web page quick data wrangling educational examples or interactive widgets that stay entirely in the browser Its less suited for heavy numerical loops file system heavy tools or situations where every millisecond of start up matters If your use case fits the interactive script profile give the verification steps a try if the initial load or package limitations feel like blockers a JavaScript first approach with a Python to JS compiler might be worth exploring
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.