← 输入框|python.land|刷新

PyScript: Run Python In Your Browser, Including Numpy And Pandas

PyScript lets you run Python in a web page. You put Python code in a <script type="py"> tag, and the visitor’s browser runs it. There’s no Python server involved, and nobody has to install anything.

This article explains what PyScript is and how it works. You’ll build a small working page, load packages like NumPy and pandas, and learn where PyScript shines and where it doesn’t. I used PyScript release 2026.7.3, the latest at the time of writing, and tested every example in Chrome.

What is PyScript

PyScript is an open source platform for running Python in the browser. Anaconda introduced it at PyCon US 2022 as a rough experiment. Since then, the project has been rebuilt from the ground up, with a new API and regular releases. With PyScript, you can:

  • Write Python inside <script type="py"> tags, or load it from a .py file.
  • Read and change the web page from Python, and react to clicks and other events.
  • Call JavaScript code, including any JavaScript library on the page.
  • Use popular Python packages, like NumPy, pandas and Matplotlib, right in the browser.
  • Choose between two Python interpreters: full CPython (through Pyodide) or the much smaller MicroPython.

How does PyScript work?

Browsers can run WebAssembly, a compact binary format that runs at close to native speed. PyScript loads a Python interpreter that was compiled to WebAssembly and hands your Python code to it. It also adds the glue you need to work with the page: functions to find HTML elements, handle events and display output.

PyScript supports two interpreters:

  • Pyodide is CPython, the regular Python you install on your computer, ported to WebAssembly. Release 2026.7.3 ships Python 3.14. The Pyodide team also compiled many packages with C code, like NumPy and pandas, so they run in the browser too. You use it with <script type="py">.
  • MicroPython is a lean reimplementation of Python 3, originally made for microcontrollers. It has a subset of the standard library and can’t use most PyPI packages, but it’s tiny and starts almost instantly. You use it with <script type="mpy">.

Here’s what Pyodide looks like on its own, as a REPL in the browser:

The Pyodide console running Python 3.14 in the browser
Pyodide REPL, running in my browser

Pyodide or MicroPython?

The difference in size is large. I measured what each of the examples in this article downloads on a first visit, with an empty browser cache:

PageDownloaded
MicroPython, hello world0.3 MB
Pyodide, hello world6.3 MB
Pyodide with NumPy and pandas14 MB

So my advice is: use Pyodide when you need real CPython behavior or packages from PyPI, especially the data science stack. Use MicroPython for small, interactive pages, and for anything that has to load fast on a phone. Browsers cache the interpreter, so later visits are much faster in both cases.

Your first PyScript page

A PyScript page is a regular HTML page that loads PyScript’s CSS and JavaScript from pyscript.net. Save this as index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Hello PyScript</title>
  <link rel="stylesheet" href="https://pyscript.net/releases/2026.7.3/core.css">
  <script type="module" src="https://pyscript.net/releases/2026.7.3/core.js"></script>
</head>
<body>
  <h1>Hello PyScript</h1>
  <input id="name" type="text" placeholder="Your name">
  <button id="greet">Greet me</button>
  <p id="output"></p>

  <script type="py">
    import sys
    from pyscript import display, when, web

    display(f"Python {sys.version.split()[0]} is running in your browser")

    @when("click", "#greet")
    def greet(event):
        name = web.page["name"].value or "stranger"
        web.page["output"].innerText = f"Hello, {name}!"
  </script>
</body>
</html>Code language: Python (python)

Here’s what happens in this page:

  • The two lines in the <head> load PyScript. The version number is part of the URL, so your page keeps working when a new release comes out. Upgrade by changing the number in both lines.
  • display() shows a value on the page, at the place of the script tag. A plain print() goes to the browser’s developer console instead.
  • The @when decorator connects the greet function to clicks on the element with id greet.
  • web.page["name"] finds the element with id name, so you can read its value or change its text.
  • You can indent the Python code inside the script tag to match your HTML. PyScript removes the common indentation before it runs the code.

Serve the page with a local web server instead of double-clicking the file, because browsers block some things on file:// pages, like loading a config file. Python has a web server built in. Run this in the folder with index.html:

python3 -m http.serverCode language: Python (python)

Then open http://localhost:8000. After a second or two, the page says “Python 3.14.2 is running in your browser”. Type a name and click the button, and the paragraph below it says “Hello, Erik!” (or whatever name you typed).

To run the same page on MicroPython, change type="py" to type="mpy". Nothing else has to change, because both interpreters share the same pyscript API. Only the first line looks odd: it says “Python 3.4.0;”, because MicroPython reports the Python version its language is based on. sys.implementation.name tells you which interpreter is running: cpython or micropython.

For anything longer than a few lines, put the Python code in its own file and point to it with src:

<script type="py" src="main.py"></script>Code language: Python (python)

Using packages like NumPy and pandas

You tell PyScript which packages to install with a configuration file. Create a file called pyscript.json next to your HTML page:

{
  "packages": ["numpy", "pandas"]
}Code language: Python (python)

And refer to it with the config attribute of your script tag:

<script type="py" config="pyscript.json">
  import numpy as np
  import pandas as pd
  from pyscript import display

  df = pd.DataFrame({
      "x": np.arange(1, 6),
      "squared": np.arange(1, 6) ** 2,
  })
  display(df)
  display(f"Mean of squared: {df['squared'].mean()}")
</script>Code language: Python (python)

PyScript installs the packages and their dependencies before it runs your code. display(df) renders the DataFrame as an HTML table, so the page shows this:

   x  squared
0  1        1
1  2        4
2  3        9
3  4       16
4  5       25
Mean of squared: 11.0Code language: Python (python)

A few things to know about the config:

  • You can write the same config in TOML, for example packages = ["numpy", "pandas"] in a file called pyscript.toml.
  • For a short JSON config, you can skip the file and put it in the attribute: config='{"packages": ["numpy"]}'.
  • Version constraints work too, like "snowballstemmer>=2.2.0".
  • The files key copies files, like a CSV or your own Python modules, into Python’s in-browser file system, so you can open() or import them.

Not every package works. With Pyodide, you can use any pure Python package from PyPI, plus the packages with C or Rust code that the Pyodide team compiled for WebAssembly. Other packages with compiled code won’t install. Check packages.pyscript.net to see if a package is supported. MicroPython can’t install from PyPI at all. It uses its own micropython-lib packages, or Python files you load with the files key.

Working with the page and JavaScript

You’ve already seen pyscript.web and @when, which are the Pythonic way to work with the page. You can also use the browser’s JavaScript objects directly. PyScript exposes window and document, and you call their methods as if they were Python objects:

<script type="py">
  from pyscript import window, document

  window.console.log("Hello from Python")
  document.title = "Changed by Python"
</script>Code language: Python (python)

This logs a message in the browser console and changes the page title. Through window, you can reach any JavaScript library you’ve loaded on the page.

One thing to watch: Python on the page runs on the browser’s main thread, the same thread that draws the page. A long calculation freezes the page until it finishes. For heavy work, add the worker attribute to the script tag, so the code runs in a background web worker. The workers guide explains how to set that up.

A Python REPL and terminal in the page

PyScript has a built-in code editor that works a bit like the Python REPL. Use type="py-editor", and the code becomes an editable box with a run button:

<script type="py-editor" config='{"packages": ["numpy"]}'>
import numpy as np
print(np.arange(10).reshape(2, 5))
</script>Code language: Python (python)

Your visitors can change the code and run it with the run button or Shift+Enter. The output appears right below the editor:

[[0 1 2 3 4]
 [5 6 7 8 9]]Code language: Python (python)

The editor only loads Python when someone clicks run for the first time, so it doesn’t slow down the page. Use type="mpy-editor" for a MicroPython editor. Editors on a page are isolated from each other unless you give them a shared env attribute. Read the editor guide for the details.

If you’d rather have a terminal, add the terminal attribute to a regular script tag. Everything you print() then shows up in a terminal on the page:

<script type="py" terminal>
print("hello world")
</script>Code language: Python (python)

For input() in the terminal, the code has to run in a worker. The terminal guide shows how.

What PyScript is good and bad at

PyScript is a good fit for:

  • Teaching and live code examples. Readers run Python without installing anything.
  • Sharing small tools, calculators and data demos as a single static page. Any static host works, because there’s no server-side Python.
  • Exploring data with pandas or NumPy in the browser, where the data never leaves the user’s computer.
  • Python developers who want an interactive front end without learning much JavaScript.

It’s a poor fit when:

  • Load time matters. Pyodide pages download several megabytes before any Python runs. MicroPython helps, but you give up most packages.
  • You need a package with compiled code that Pyodide doesn’t support.
  • Your code needs the network the way it does on a computer. The browser sandbox doesn’t allow raw sockets, so libraries that open their own connections don’t work. Web requests go through the browser’s fetch, and the server has to allow them (CORS).
  • You need the user’s file system. Python sees a virtual, in-memory file system. Files you write disappear when the page reloads.
  • You need to keep code or secrets private. Everything runs in the visitor’s browser, so they can read all of it. Database passwords and API keys belong on a server.

PyScript doesn’t replace a backend framework like Django or Flask. It runs Python on the other side of the connection, in the browser.

Examples and next steps

The best way to see what PyScript can do is to look at working apps:

If you’re new to Python itself, learn the language first on your own computer: install Python and start with the Python REPL. Everything you learn there works in PyScript too.

Leave a Comment