pyscript
PyScript is an open source platform for Python in the browser. Try PyScript: https://pyscript.com Examples: https://tinyurl.com/pyscript-examples Community: https://discord.gg/HxvBtukrg2
Top Related Projects
Pyodide is a Python distribution for the browser and Node.js based on WebAssembly
Brython (Browser Python) is an implementation of Python 3 running in the browser
Skulpt is a Javascript implementation of the Python programming language
Literate scientific computing and communication for the web
Wasm powered Jupyter running in the browser 💡
Quick Overview
PyScript is an open-source framework that allows users to create Python applications in the browser using HTML. It enables the execution of Python code directly in web pages, combining the power of Python with the accessibility of web technologies.
Pros
- Allows running Python code in the browser without server-side processing
- Integrates seamlessly with HTML, making it easy to add Python functionality to web pages
- Provides access to popular Python libraries and frameworks in the browser
- Enables rapid prototyping and development of interactive web applications
Cons
- Still in early development stages, which may lead to stability issues and frequent changes
- Performance can be slower compared to native JavaScript applications
- Limited browser support, especially for older versions
- Larger initial load times due to downloading the Python runtime and dependencies
Code Examples
- Basic PyScript usage:
<py-script>
print("Hello, PyScript!")
</py-script>
- Using Python libraries:
<py-env>
- numpy
- matplotlib
</py-env>
<py-script>
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
</py-script>
- Interacting with HTML elements:
<button id="myButton">Click me!</button>
<py-script>
from js import document
def on_click(event):
print("Button clicked!")
document.getElementById("myButton").addEventListener("click", on_click)
</py-script>
Getting Started
To use PyScript in your web page:
- Add the following lines to your HTML file's
<head>
section:
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
- Write your Python code within
<py-script>
tags in the HTML body:
<py-script>
print("Hello, World!")
</py-script>
- Open the HTML file in a modern web browser to see the result.
Competitor Comparisons
Pyodide is a Python distribution for the browser and Node.js based on WebAssembly
Pros of Pyodide
- More mature and established project with a larger ecosystem
- Offers direct access to Python's scientific computing stack (NumPy, Pandas, etc.)
- Provides a REPL for interactive Python execution in the browser
Cons of Pyodide
- Larger file size and longer initial load time
- Requires more setup and configuration for integration into web applications
- Less focus on seamless HTML integration compared to PyScript
Code Comparison
PyScript:
<py-script>
print("Hello, PyScript!")
</py-script>
Pyodide:
pyodide.runPython(`
print("Hello, Pyodide!")
`)
PyScript provides a more HTML-centric approach, allowing Python code to be embedded directly in HTML using custom tags. Pyodide, on the other hand, requires JavaScript to execute Python code, offering more flexibility but with additional setup.
Both projects aim to bring Python to the web browser, but they cater to slightly different use cases. PyScript focuses on simplicity and ease of use for web developers, while Pyodide provides a more comprehensive Python runtime environment with access to scientific libraries, making it suitable for more complex applications and data analysis in the browser.
Brython (Browser Python) is an implementation of Python 3 running in the browser
Pros of Brython
- More mature project with longer development history
- Supports a wider range of Python libraries and modules
- Better performance for complex Python operations
Cons of Brython
- Larger file size, which can impact page load times
- Less seamless integration with HTML compared to PyScript
- Steeper learning curve for developers new to Python in the browser
Code Comparison
Brython:
from browser import document, alert
def greet(ev):
alert(f"Hello, {document['name'].value}!")
document['greet'].bind('click', greet)
PyScript:
<py-script>
def greet(event):
name = document.querySelector("#name").value
alert(f"Hello, {name}!")
document.querySelector("#greet").addEventListener("click", greet)
</py-script>
Both Brython and PyScript aim to bring Python to the browser, but they differ in their approach and implementation. Brython focuses on providing a more complete Python environment, while PyScript emphasizes ease of use and integration with HTML. The choice between the two depends on the specific requirements of your project and your familiarity with Python and web development.
Skulpt is a Javascript implementation of the Python programming language
Pros of Skulpt
- Lightweight and faster execution for simple Python programs
- More mature project with longer development history
- Better support for older browsers and legacy systems
Cons of Skulpt
- Limited support for advanced Python features and libraries
- Less active development and community engagement
- Focused primarily on educational use cases
Code Comparison
Skulpt:
import skulpt
def greet(name):
print("Hello, " + name + "!")
greet("World")
PyScript:
<py-script>
def greet(name):
print(f"Hello, {name}!")
greet("World")
</py-script>
PyScript offers a more modern approach, integrating directly with HTML and providing access to a wider range of Python libraries. It aims to bring the full power of Python to the browser, including support for data science and visualization tools.
Skulpt, on the other hand, focuses on providing a lightweight Python environment for educational purposes, with faster execution for simple programs but limited advanced features.
While Skulpt has a longer history, PyScript is gaining momentum with its more comprehensive approach to running Python in the browser, backed by Anaconda and the broader Python community.
Literate scientific computing and communication for the web
Pros of Iodide
- More mature project with a longer development history
- Supports multiple programming languages, not just Python
- Includes built-in data visualization tools
Cons of Iodide
- Less active development and community support
- More complex setup and configuration process
- Limited integration with external libraries compared to PyScript
Code Comparison
Iodide:
%% js
var data = [1, 2, 3, 4, 5];
var chart = new Chart(ctx, {
type: 'bar',
data: {labels: data, datasets: [{data: data}]}
});
PyScript:
import matplotlib.pyplot as plt
data = [1, 2, 3, 4, 5]
plt.bar(range(len(data)), data)
plt.show()
Both projects aim to bring interactive programming to the web browser, but they take different approaches. Iodide offers a more comprehensive environment for data science and visualization, supporting multiple languages. PyScript focuses on bringing Python specifically to the web, with easier setup and stronger integration with Python libraries. The code examples show how data visualization can be achieved in both environments, with Iodide using JavaScript and PyScript leveraging Python's matplotlib library.
Wasm powered Jupyter running in the browser 💡
Pros of JupyterLite
- Provides a full Jupyter notebook environment in the browser
- Supports multiple kernels (Python, JavaScript, R)
- Offers a familiar interface for Jupyter users
Cons of JupyterLite
- Larger file size and potentially slower initial load times
- More complex setup and configuration compared to PyScript
- Limited to Jupyter-specific features and workflows
Code Comparison
PyScript:
<py-script>
print("Hello, PyScript!")
import numpy as np
data = np.random.rand(10)
print(data.mean())
</py-script>
JupyterLite:
# In a Jupyter notebook cell
print("Hello, JupyterLite!")
import numpy as np
data = np.random.rand(10)
print(data.mean())
Both examples achieve similar results, but PyScript integrates directly into HTML, while JupyterLite runs within a notebook environment. PyScript offers more flexibility for web integration, while JupyterLite provides a more comprehensive scientific computing environment.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PyScript
PyScript is an open source platform for Python in the browser.
Using PyScript is as simple as:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>PyScript!</title>
<link
rel="stylesheet"
href="https://pyscript.net/snapshots/2024.9.2/core.css"
/>
<script
type="module"
src="https://pyscript.net/snapshots/2024.9.2/core.js"
></script>
</head>
<body>
<!-- Use MicroPython to evaluate some Python -->
<script type="mpy" terminal>
print("Hello, world!")
</script>
</body>
</html>
PyScript enables the creation of rich Python applications in the browser using Pyodide (a version of CPython), MicroPython, WASM, and modern web technologies. It means Python now runs anywhere a browser runs: desktop, laptop, mobile, tablet, or any other browser enabled device.
To start building, read the Beginning PyScript tutorial.
For example applications, see here.
Other useful resources:
- Our Home Page as an open source project.
- The official technical docs.
- A YouTube channel with helpful videos and community content.
- A free-to-use online IDE for trying PyScript.
- Our community Discord Channel, to keep in touch .
Every Tuesday at 15:30 UTC there is the PyScript Community Call on zoom, where we can talk about PyScript development in the open. Most of the maintainers regularly participate in the call, and everybody is welcome to join. This meeting is recorded and uploaded to our YouTube channel.
Every other Thursday at 16:00 UTC there is the PyScript FUN call: the focus of this call is to share fun projects, goofy hacks or clever uses of PyScript. It's a supportive, energetic and entertaining meeting. This meeting is also recorded and uploaded to our YouTube channel.
For more details on how to join the calls and up to date schedule, consult the official calendar:
- Google calendar in UTC time;
- iCal format.
Contribute
For technical details of the code, please see the README in
the core
directory.
Read the contributing guide to learn about our development process, reporting bugs and improvements, creating issues and asking questions.
Check out the development process documentation for more information on how to setup your development environment.
Governance
The PyScript organization governance is documented in a separate repository.
Supporters
PyScript is an independent open source project.
However, PyScript was born at Anaconda Inc and its core contributors are currently employed by Anaconda to work on PyScript. We would like to acknowledge and celebrate Anaconda's continued support of this project. Thank you Anaconda Inc!
Top Related Projects
Pyodide is a Python distribution for the browser and Node.js based on WebAssembly
Brython (Browser Python) is an implementation of Python 3 running in the browser
Skulpt is a Javascript implementation of the Python programming language
Literate scientific computing and communication for the web
Wasm powered Jupyter running in the browser 💡
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot