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

PyScript allows you to create rich Python applications in the browser using nothing but HTML and Python code. Although it’s super rough and super new, I can tell you this will be a game-changing part of the Python ecosystem. It’s worth keeping a close watch on this one!

This article explains what PyScript is and how it works. I also included a fully working PyScript REPL for you to play with.

What is PyScript

PyScript was introduced on PyCon US 2022 by a company well known in the Python world: Anacona. The aim is to allow Python in the context of your browser. PyScript allows you to:

  • Write Python code between specific <py-script> </py-script> tags
  • Interact with JavaScript code, including all the available JavaScript libraries.
  • Run popular Python packages right in your browser. Some packages that are available include:
    • Numpy
    • Pandas
    • pyyaml
  • Manage the environment. You can define which packages to include on a page.
  • Create visual applications with buttons, text boxes, containers, etcetera.

How does PyScript work?

PyScript runs in your browser, which is pretty amazing! Under the hood, PyScript uses a project called Pyodide. Pyodide is a port of CPython to WebAssembly. This project did not just port CPython to web assembly, but also made the effort to compile well-known packages like Pandas and Numpy so they can be run right from the browser.

In the following screenshot, you can see the actual Python REPL running in my browser:

Pyodide REPL
Pyodide REPL, running in my browser

What does PyScript add to Pyodide?

So why is PyScript such a big deal? It’s mostly because it makes using Python in the browser more accessible. All you need to do, is include some JavaScript and you’re ready to use the py-script tags to include Python code in your HTML page.

Another thing I noticed, is that PyScript is trying hard to make Python interoperate with the browser. The Pyodide REPL doesn’t support the input() function, for example. PyScript does, by means of a simple JavaScript popup box asking for your input.

The PyScript REPL

The following REPL runs entirely in your browser, thanks to the awesome PyScript project. PyScript is new and experimental and was only tested in recent browsers so far. However, I’ve found it works in Firefox and anything based on Chrome, like Chrome itself, Chromium, and Microsoft Edge.

Usage instructions

Note that this resembles a regular Python REPL, but it’s not at all the same as the real Python REPL. For example, you need to enter your code and hit shift+enter or the play button to run it. It doesn’t support the underscore to get the previous result. It does support the input() function though!

The REPL

I’ve embedded the REPL below. If you don’t see anything, it probably means your browser is not supported:

Get certified with our courses

Learn Python properly through small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

Leave a Comment

Python Newsletter

Before you leave, you may want to sign up for my newsletter.

Tips & Tricks
News
Course discounts

Googler exit intent popup

No spam & you can unsubscribe at any time.