Intro to PyScript: Run Python in your web browser (2025)

PyScript lets you run Python scripts right in the browser, side by side with JavaScript, with two-way interaction between your code and the web page.

Intro to PyScript: Run Python in your web browser (1)

Credit: dTosh / Shutterstock

Created by Anaconda and launched in April 2022, PyScript is an experimental but promising new technology that makes the Python runtime available as a scripting language in WebAssembly-enabled browsers.

Every commonly used browser now supports WebAssembly, the high-speed runtime standard that languages like C, C++, and Rust can compile to. Python’s reference implementation is written in C, and one earlier project, Pyodide, provided a WebAssembly port of the Python runtime.

PyScript, though, aims to provide a whole in-browser environment for running Python as a web scripting language. It builds on top of Pyodide but adds or enhances features like the ability to import modules from the standard library, use third-party imports, configure two-way interactions with the Document Object Model (DOM), and do many other things useful in both the Python and JavaScript worlds.

Right now, PyScript is still a prototypical and experimental project. Anaconda doesn’t recommend using it in production. But curious users can try out examples on the PyScript site and use the available components to build experimental Python-plus-JavaScript applications in the browser.

In this article, we’ll take a tour of PyScript, and see how it facilitates Python and JavaScript interactions in your web apps.

Programming with PyScript

At its core, PyScript consists of a single JavaScript include that you can add to a web page. This include loads the base PyScript runtime and automatically adds support for custom tags used in PyScript.

Here’s a simple example of a “Hello, world” project in PyScript:

<!DOCTYPE html><html><head> <link rel="stylesheet" href="https://pyscript.net/releases/2023.11.2/core.css" /> <script type="module" src="https://pyscript.net/releases/2023.11.2/core.js"> </script></head><body> <script type="py" terminal> from pyscript import display display("Hello World!") print("Hello terminal!") </script></body></html>

The script tag in the document’s head loads the core PyScript functionality. The .css stylesheet is optional, but useful. Among other things, it inserts notices to the user at the page’s load time about what the page is doing—loading the Python runtime, initializing, and so on.

Python code is enclosed in thescript tag with atype="py" attribute. Note that the code should be formatted according to Python’s conventions for indentation, or it won’t run properly. Be aware of this if you use an editor that reformats HTML automatically; it might mangle the contents of the script block and make it unrunnable. You can also refer to a .py file rather than include the script inline, which may be easier.

Any Python code is evaluated once the PyScript components finish loading. You can choose whether the output is sent to the DOM (use pyscript.display), or to an embedded terminal. If you use the terminal, you have to include terminal as an attribute on the script tag. (More about this below.)

If the script in the tags writes to stdout (as with a print statement), you can direct where you want the output displayed on the page by supplying an output property. In this example, stdout for the script gets directed to the div with the ID of "out".

If you save this into a file and open it in a web browser, you’ll first see a “loading” indicator and a pause, as the browser obtains the PyScript runtime and sets it up. The runtime should remain cached on future loads but will still take a moment to activate. After that, Hello world should appear on the page twice—once at the top in HTML, and once in a black pane that is the embedded terminal.

Standard library imports

Scripts using Python’s builtins alone are only somewhat useful. Python’s standard library is available in PyScript the same way you’d use it in regular Python: simply import and get to work. Standard library imports should just work in PyScript.

If you wanted to modify the above script block to display the current time, you wouldn’t need to do it any differently than you would in conventional Python:

import datetimeprint ("Current date and time:", datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))

Using libraries from PyPI

What if you want to install a package from PyPI and use that? PyScript lets you specify project configurations, including any third-party packages to be installed from PyPI, by way of a .toml or .json format file in your project’s directory. Let’s see how it works using .toml.

To use the project config file, you’ll need to include the configdirective in your scripttag:

<script type="py" src="main.py" config="pyscript.toml">

The pyscript.tomlfile lists any needed packages:

packages = ["package","another-package"]

Note that not all packages from PyPI will install and run as expected. Most “pure” Python packages, likehumanize, should run fine. And packages used in the examples provided by Anaconda, like numpy, pandas, bokeh, or matplotlib, will also work. But packages that require network access or work with platform-native elements like GUIs are not likely to work.

Importing locally

For another common scenario, let’s say you want to import from other Python scripts in the same directory tree as your web page. Using imports makes it easier to move more of your Python logic out of the web page itself, where it’s intermixed with your presentation and may become difficult to work with.

Normally, Python uses the presence of other .py files in the file system to indicate what it can import. PyScript doesn’t work this way, so you’ll need to specify which files you want to make available as importable modules.

To do this, you list the URLs you want to make available to PyScript in your application’s config file in a [files] block, along with how you want to map them to PyScript’s emulated filesystem. For instance:

[files]"/module.py" = "./libs/module.py""https://mydata.com/data.csv" = "./data.csv"

Whatever file is accessible from the URL on the left is made available to the Python interpreter’s emulated filesystem via the path on the right. In this case, the file you’d see if you browsed to/module.py (e.g., http://localhost:8000/module.py) is available to Python as libs.module. Likewise, the file at the URLhttps://mydata.com/data.csvis available in the emulated current working directory asdata.csv.

The in-browser terminal

Python users should be familiar with the REPL, the console interface to the Python runtime. In PyScript, you can embed into the browser a live terminal running the REPL, or just console output from your Python program.

To embed a terminal, use a script tag that has terminal as one of its attributes:

<script type="py" terminal>print("hello world")</script>

This opens a terminal and prints hello world, but does not allow any interactivity. For interaction, you need to use the worker attribute:

<script type="py" terminal worker>name = input("What is your name? ")print(f"Hello, {name}")</script>

The worker runs your program in a web worker, which is essentially a subprocess. Note that you cannot use web workers on an HTML file loaded locally; you must load it from a web server that provides certain headers. PyScript’s documentation explains how this works in detail.

Most of what’s possible in a regular console, like coloring and Unicode, should be supported in the PyScript terminal, too.

Interacting with the DOM and JavaScript

Because PyScript is based on browser technology, it has mechanisms forinteracting with the DOM. For instance, if you wanted to get the value of an input box on a web page and use it in your Python code, you’d do this:

from pyscript import window, documentinputbox = document.querySelector("#my-input")print("Value of input:", inputbox.value)

PyScript also includes a module called pydom that allows dynamic creation of objects on the page:

from pyweb import pydomnew_div = pydom.create("div")new_div.content = "Hello World"

This creates a new div element on the page and populates it with text. Most of the other kinds of manipulations you can perform with the DOM in JavaScript—such as adding elements and changing attributes—can be done through the pydom library.

Related content

  • analysisBeyond the usual suspects: 5 fresh data science tools to try today The mid-month report includes quick tips for easier Python installation, a new VS Code-like IDE just for Python and R users, and five newer data science tools you won't want to miss.By Serdar YegulalpJul 12, 20242 minsPythonProgramming LanguagesSoftware Development
  • analysisGenerative AI won’t fix cloud migration You’ve probably heard how generative AI will solve all cloud migration problems. It’s not that simple. Generative AI could actually make it harder and more costly. By David LinthicumJul 12, 20245 minsGenerative AIArtificial IntelligenceCloud Computing
  • newsHR professionals trust AI recommendations HireVue survey finds 73% of HR professionals trust AI to make candidate recommendations, while 75% of workers are opposed to AI making hiring decisions. By Paul KrillJul 11, 20243 minsTechnology IndustryCareers
  • how-toSafety off: Programming in Rust with `unsafe` What does it mean to write unsafe code in Rust, and what can you do (and not do) with the 'unsafe' keyword? The facts may surprise you.By Serdar YegulalpJul 11, 20248 minsRustProgramming LanguagesSoftware Development
  • Resources
  • Videos
Intro to PyScript: Run Python in your web browser (2025)

FAQs

What is PyScript in Python? ›

PyScript is an open source platform for Python in the browser. PyScript brings together two of the most vibrant technical ecosystems on the planet. If the web and Python had a baby, you'd get PyScript. At the core of PyScript is a philosophy of digital empowerment.

How do I run a Python project on a website? ›

Run the application

Follow these steps to run your web application: In Solution Explorer, right-click the app.py file and select Set as Startup File. This command identifies the code file to launch in Python when running the app. Right-click the project in Solution Explorer and select Properties.

How to link PyScript to HTML? ›

As its name suggest, we can embed the Python code within <py-script> tag in an HTML. The <py-script> tag can be added in the <head> or <body> tag depending on the task at hand. In this approach, we write the code with . py extension, which can be then reference in the <py-script> tag using the src attribute.

Can you run a Python script on Chrome? ›

Running a Python Program Using the Chrome Extension

The method is simple, Go to the chrome web store to download the Python Shell Chrome Extension. Click on add to chrome to install and add the extension to the browser. After the extension is installed, click on it to launch it.

How can I run a Python script in HTML? ›

To run a Python script from an HTML button, you can use the following steps:
  1. Create a Python script that you want to run.
  2. Save the Python script in a file with a . py extension.
  3. Create an HTML file with a button.
  4. Add the following JavaScript code to the HTML file:
Sep 26, 2023

Can I use PyScript instead of JavaScript? ›

In other words, PyScript allows you to use Python, with or without JavaScript, to build interactive websites that don't necessarily have to communicate with a server.

Does PyScript work offline? ›

Sometimes you want to run PyScript applications offline. Both PyScript core and the interpreter used to run code need to be served with the application itself. Two requirements are needed to create an offline PyScript are: Download and include PyScript core.

What websites can run Python code? ›

Websites like Replit, Google Colab, Jupyter Notebook on Binder, and CodeSandbox support running Python code online. These platforms offer collaborative environments with varying features for coding, debugging, and sharing Python projects.

Can you run a website with Python? ›

Frameworks – Python has tons of Frameworks like Django and Flask, they provide powerful and efficient tools for building web applications. This is a huge plus for Python. It basically allows the developer to develop websites and complex web applications very easily.

How to integrate Python with HTML? ›

Integrating Python and HTML can be done in various ways, depending on what you want to achieve. For example, you could: Create HTML with Jinja2 templates and a Python web framework like Django. Use dynamic HTML generation with Flask or another Python web framework by using a combination of HTML, CSS, and Python code.

Can I run Python on a browser? ›

Using Python in a browser is fine for learning and fiddling around. However, once you start to get serious about Python development, you will find out that, at some point, you'll want a proper IDE (Integrated Development Environment) and a local installation of Python.

Is PyScript production ready? ›

PyScript, an alpha stage tool, makes it easier to use Python scripts in HTML and its scientific stack on the client side, though it's not yet production-ready. As you continue to build your PyScript apps, consider boosting your build efficiency with Earthly, your new favorite tool for reproducible builds.

Is PyScript secure? ›

Secure: PyScript runs in the world's most battle-tested secure platform, the browser! Powerful: the best of the web and Python, together at last.

How to use Python with web? ›

These are some steps you can take to do Python web development:
  1. Learn HTML and CSS. Learning programming languages such as HyperText Markup Language (HTML) and Cascading Style Sheets (CSS), can help you design and implement websites. ...
  2. Learn JavaScript. ...
  3. Learn DOM and jQuery. ...
  4. Use web frameworks.
Feb 16, 2024

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 5665

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.