PyScript

From Wikipedia, the free encyclopedia

PyScript is a framework for running Python scripts in a web browser.[4][5] PyScript was presented to the public by Peter Wang at a Python developer conference in June 2022.[6][7] PyScript is primarily developed by Anaconda, Inc.

DeveloperAnaconda, Inc.
Stable release
2026.3.1[1] Edit this on Wikidata / 2 March 2026; 9 days ago (2 March 2026)
OScross-platform
LicenseApache Software License 2.0[2]
Quick facts Developer, Stable release ...
PyScript
DeveloperAnaconda, Inc.
Stable release
2026.3.1[1] Edit this on Wikidata / 2 March 2026; 9 days ago (2 March 2026)
OScross-platform
LicenseApache Software License 2.0[2]
Websitepyscript.net
Influenced by
Python[3]
Close

Usage

PyScript allows Python scripts, which normally run on a server, to be executed in a web browser.[8] This allows the same programming language to be used in the backend (on the server) and in the frontend (in the web browser). PyScript is a TypeScript library that is loaded by the web page and interprets the embedded Python scripts. Apart from PyScript's JavaScript library and CSS rules, there are no other dependencies. JavaScript is typically used to provide dynamic functionality to a web page. PyScript can replace JavaScript, but it can also run side-by-side with JavaScript. This allows JavaScript to call PyScript functions and vice versa. The advantage of PyScript lies in the large number of available Python libraries that can be used on web pages. These must be installed on the server.[9] For example, there are graphics libraries for editing digital images. The image data does not have to be sent to the server for processing. Another example is the creation of graphs and diagrams.[10] The Python programming language was mainly used on the server side. There has been virtually no support for pure front-end development so far. PyScript is intended to help close this gap.[11][12]

In order to execute Python scripts in a browser, you need a Python runtime environment. Several Python runtime environments exist, typically written in C. PyScript can theoretically work with various runtime environments. By default, Pyodide (a port of the CPython runtime environment to WebAssembly) is used.[7] Many well-known Python packages have also been ported to WebAssembly and can therefore be used by PyScript. Pyodide was developed in 2018 by Michael Droettboom at the Mozilla Foundation.[13] To use Pyodide, and thus PyScript, in a browser, Firefox version 70.0 or Google Chrome version 71.0 or higher is required.[14]

Examples

Example: What time is it?

The following example imports three packages. These contain functions for output control, date management, and event handling. Each time the button is clicked, the current date and time are displayed in a new line.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <title>PyScript: What time is it?</title>
    <link rel="stylesheet" href="https://pyscript.net/releases/2025.10.3/core.css">
    <script defer type="module" src="https://pyscript.net/releases/2025.10.3/core.js"></script>
  </head>
  <body>
    <button id="btnTime">What time is it?</button>
    <script type="py">
      from pyscript import when, display
      from datetime import datetime
      @when("click", "#btnTime")
      def click_handler(event):
        now = datetime.now()
        display(
          now.strftime(
            "Today is %m/%d/%Y. " +
            "The current time is %I:%M:%S %p."
          )
        )
    </script>
  </body>
</html>

As an alternative to <script type="py">, the element <py-script> can also be used (as in the example below). However, the new notation is recommended.[15]

PyScript also offers the possibility to communicate with the user via a command prompt. For this, the terminal attribute must be added to the <script type="py"> or <py-script> element. If input should also be possible, the worker attribute must be specified as well.[16] In earlier versions, a terminal could also be implemented using the <py-terminal> element.

Example: Bar chart

This example shows how to create a bar chart.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <title>PyScript: Bar chart</title>
    <link rel="stylesheet" href="https://pyscript.net/releases/2025.10.3/core.css">
    <script defer type="module" src="https://pyscript.net/releases/2025.10.3/core.js"></script>
  </head>
  <body>
    <div id="mpl"></div>
    <py-config>
      packages = ["matplotlib"]
    </py-config>
    <py-script>
      import matplotlib.pyplot as plt
      from pyscript import display
      fig, ax = plt.subplots()
      x = ["Apples", "Pears", "Bananas", "Pineapples"]
      y = [3, 7, 8, 10]
      plt.bar(x, y)
      plt.xlabel('Fruits')
      plt.ylabel('Popularity')
      plt.title('Fruits and their popularity')
      fig
      display(fig, target="mpl")
    </py-script>
  </body>
</html>

In order to create such diagrams, the matplotlib extension must be loaded in an <py-config> block.

Literature

  • Merkert, Pina (2022). c’t Python: PyScript im Browser statt JavaScript (in German). Heise Group. p. 30. ISBN 978-3-95788-318-6.
  • Divadkar, Varun (2024). "The PyScript Framework". Learn Autonomous Programming with Python. BPB Publications. pp. 257–274. ISBN 978-93-5551763-0.

References

Related Articles

Wikiwand AI