Convert Figma logo to code with AI

brython-dev logobrython

Brython (Browser Python) is an implementation of Python 3 running in the browser

6,357
507
6,357
61

Top Related Projects

3,325

Skulpt is a Javascript implementation of the Python programming language

11,971

Pyodide is a Python distribution for the browser and Node.js based on WebAssembly

17,824

Try PyScript: https://pyscript.com Examples: https://tinyurl.com/pyscript-examples Community: https://discord.gg/HxvBtukrg2

A Python Interpreter written in Rust

Quick Overview

Brython is a Python 3 implementation that runs in the browser. It allows developers to write client-side web applications using pure Python, without the need for JavaScript. Brython provides a comprehensive set of tools and libraries for building dynamic web applications, including support for DOM manipulation, event handling, and AJAX.

Pros

  • Python Syntax: Brython allows developers to write client-side code using the familiar Python syntax, making it easier for Python developers to transition to web development.
  • Cross-Platform: Brython is a cross-platform solution, allowing developers to write code that can run on a variety of browsers and devices.
  • Extensive Library Support: Brython comes with a wide range of built-in modules and libraries, including support for common web development tasks such as DOM manipulation, AJAX, and more.
  • Interoperability with JavaScript: Brython provides a seamless integration with JavaScript, allowing developers to use existing JavaScript libraries and frameworks within their Brython applications.

Cons

  • Browser Compatibility: While Brython is designed to be cross-platform, it may not work consistently across all browsers, especially older versions or less popular browsers.
  • Performance: Depending on the complexity of the application, Brython may not perform as well as native JavaScript, especially for resource-intensive tasks.
  • Ecosystem: Compared to the vast JavaScript ecosystem, the Brython ecosystem is relatively smaller, which may limit the availability of third-party libraries and tools.
  • Learning Curve: Developers who are new to both Python and web development may face a steeper learning curve when working with Brython, as they need to learn both the language and the web development concepts.

Code Examples

Here are a few examples of how to use Brython:

  1. DOM Manipulation:
from browser import document, alert

def change_color(event):
    document["myDiv"].style.backgroundColor = "blue"

document["myButton"].bind("click", change_color)

This code demonstrates how to manipulate the DOM using Brython. It binds a click event to a button, which changes the background color of a div element.

  1. AJAX Request:
from browser import ajax

def on_complete(req):
    if req.status == 200 or req.status == 0:
        alert(req.text)
    else:
        alert(f"Error {req.status}")

req = ajax.Ajax()
req.open("GET", "/data.json", True)
req.bind("complete", on_complete)
req.send()

This code shows how to make an AJAX request using Brython. It sends a GET request to the /data.json endpoint and displays the response in an alert.

  1. Event Handling:
from browser import document, alert

def handle_keypress(event):
    alert(f"You pressed the {event.key} key!")

document.bind("keypress", handle_keypress)

This code demonstrates how to handle keyboard events in a Brython application. It binds a keypress event to the document, and displays an alert with the pressed key.

Getting Started

To get started with Brython, follow these steps:

  1. Include the Brython script in your HTML file:
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython/dist/brython.min.js"></script>
  1. Initialize Brython in your HTML file:
<script>
    window.onload = function() {
        brython();
    }
</script>
  1. Create a Python script (e.g., app.py) and write your Brython code:
from browser import document, alert

def say_hello(event):
    alert("Hello, Brython!")

document["myButton"].bind("click", say_hello)
  1. In your HTML file, add a reference to your Python script:
<script type="text/python" src="app.py"></script>
  1. Open your HTML file in a web browser, and you should see your Brython application running.

Competitor Comparisons

3,325

Skulpt is a Javascript implementation of the Python programming language

Pros of Skulpt

  • Skulpt is a full-featured Python interpreter that runs entirely in the browser, allowing for client-side execution of Python code.
  • Skulpt has a large and active community, with regular updates and improvements.
  • Skulpt provides a comprehensive set of Python standard library modules, making it a viable option for a wide range of use cases.

Cons of Skulpt

  • Skulpt has a larger codebase and footprint compared to Brython, which may impact performance and load times in some scenarios.
  • Skulpt's focus on full-featured Python support may make it more complex to set up and configure for simpler use cases.
  • Skulpt's development is primarily driven by a single organization, which could potentially limit the diversity of contributions and features.

Code Comparison

Brython:

from browser import document, alert

def say_hello(event):
    alert("Hello, world!")

document["mybutton"].bind("click", say_hello)

Skulpt:

import sys
from io import StringIO

def hello():
    print("Hello, world!")

old_stdout = sys.stdout
sys.stdout = buffer = StringIO()
hello()
sys.stdout = old_stdout
print(buffer.getvalue())
11,971

Pyodide is a Python distribution for the browser and Node.js based on WebAssembly

Pros of Pyodide

  • Pyodide is a full-featured Python environment that can run in the browser, allowing for client-side Python execution.
  • Pyodide provides a comprehensive set of scientific and data analysis libraries, making it well-suited for data-driven web applications.
  • Pyodide's integration with WebAssembly enables efficient and fast execution of Python code in the browser.

Cons of Pyodide

  • Pyodide has a larger initial download size compared to Brython, which can impact page load times.
  • Pyodide's dependency on WebAssembly may limit its compatibility with older browsers.
  • Pyodide's focus on scientific and data-related libraries may make it less suitable for general-purpose web development tasks.

Code Comparison

Brython:

from browser import document, alert

def say_hello(event):
    alert("Hello, world!")

document <= "<button>Click me</button>"
document["button"].bind("click", say_hello)

Pyodide:

import js
from pyodide import create_proxy

def say_hello(event):
    js.alert("Hello, world!")

button = js.document.createElement("button")
button.textContent = "Click me"
button.addEventListener("click", create_proxy(say_hello))
js.document.body.appendChild(button)

Both examples create a simple button that displays an alert when clicked, but the Pyodide version requires more boilerplate code to interact with the JavaScript DOM.

17,824

Try PyScript: https://pyscript.com Examples: https://tinyurl.com/pyscript-examples Community: https://discord.gg/HxvBtukrg2

Pros of pyscript

  • pyscript is a more recent project, with active development and a growing community.
  • pyscript provides a more streamlined and user-friendly experience for running Python in the browser, with a focus on simplicity and ease of use.
  • pyscript has better integration with modern web frameworks and tools, such as Jupyter Notebook and Streamlit.

Cons of pyscript

  • pyscript has a smaller ecosystem and fewer resources available compared to Brython.
  • pyscript may have fewer features and customization options than Brython, which has been around for longer.
  • pyscript's performance and compatibility with older browsers may not be as robust as Brython's.

Code Comparison

Brython:

from browser import document, alert

def say_hello(event):
    alert("Hello, world!")

document["mybutton"].bind("click", say_hello)

pyscript:

import js

def say_hello(event):
    js.alert("Hello, world!")

pyscript.write_to_element("mybutton", "click", say_hello)

Both examples demonstrate how to create a simple button that displays an alert when clicked, but the syntax and approach differ between the two libraries.

A Python Interpreter written in Rust

Pros of RustPython

  • Performance: RustPython is written in Rust, a systems programming language known for its performance and safety. This can result in faster execution times compared to Brython, which is written in JavaScript.
  • Concurrency: Rust's built-in support for concurrency and parallelism can potentially enable RustPython to better leverage modern hardware and improve overall performance.
  • Portability: Rust's cross-compilation capabilities can make RustPython more portable across different platforms and architectures.

Cons of RustPython

  • Ecosystem: Brython has a larger and more established ecosystem, with more third-party libraries and tools available for Python developers.
  • Learning Curve: Developers familiar with Python may need to invest more time in learning Rust, which has a different syntax and programming paradigm compared to Python.
  • Maturity: Brython has been around for longer and may have a more stable and feature-complete implementation of the Python language.

Code Comparison

Brython (Python):

def hello(name):
    print(f"Hello, {name}!")

hello("World")

RustPython (Rust):

fn hello(name: &str) {
    println!("Hello, {}!", name);
}

fn main() {
    hello("World");
}

The code snippets above demonstrate a simple "Hello, World!" program in both Brython (Python) and RustPython (Rust). The overall structure and functionality are similar, but the syntax and language-specific features differ.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

brython

Brython (Browser Python) is an implementation of Python 3 running in the browser, with an interface to the DOM elements and events.

Here is a simple example of an HTML page running Python:

<html>

    <head>
        <script type="text/javascript" src="/path/to/brython.js"></script>
    </head>

    <body>

        <script type="text/python">
        from browser import document, alert

        def echo(event):
            alert(document["zone"].value)

        document["mybutton"].bind("click", echo)
        </script>

        <input id="zone"><button id="mybutton">click !</button>

    </body>

</html>

To use Brython, all there is to do is:

  1. Load the script brython.js.
  2. Write Python code inside tags <script type="text/python">.

Main features

Brython supports the syntax of Python 3, including comprehensions, generators, metaclasses, imports, etc. and many modules of the CPython distribution.

Since version 3.8.0, Brython implements the Python version of the same major / minor version number.

It includes libraries to interact with DOM elements and events, and with existing Javascript libraries such as jQuery, D3, Highcharts, Raphael etc. It supports the latest specs of HTML5/CSS3, and can use CSS Frameworks like Bootstrap3, LESS, SASS etc.

Getting started

Zero install !

The most simple way to get started, without anything to install, is to use the distribution available online through jsDelivr. You can choose the latest stable release :

<script type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/brython@3.12.4/brython.min.js">
</script>

The previous code will allow you to use raw python code, but if you import modules from the standard library you have to load a single javascript file with the available stdlib:

<script type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/brython@3.12.4/brython_stdlib.js">
</script>

jsDelivr supports version ranges, so if you want the latest of the 3.12.x versions:

<script type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/brython@3.12/brython.min.js">
</script>
<script type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/brython@3.12/brython_stdlib.js">
</script>

or the latest of the 3.x.y versions:

<script type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/brython@3/brython.min.js">
</script>
<script type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/brython@3/brython_stdlib.js">
</script>

If you want to use the latest development version, you can load these scripts instead:

<script src="https://raw.githack.com/brython-dev/brython/master/www/src/brython.js"
        crossorigin="anonymous">
</script>
<script src="https://raw.githack.com/brython-dev/brython/master/www/src/brython_stdlib.js"
        crossorigin="anonymous">
</script>

Local install

To install Brython locally, if you have a CPython distribution with pip :

pip install brython

then create a new directory and run

brython-cli install

or by loading the latest version of the Brython zip file from the releases page.

In both cases, the distribution includes brython.js (the core Brython engine) and brython_stdlib.js (a bundle of all the files in the standard distribution).

It also includes the page demo.html that shows a few examples of how you can interact with a web page using Python as the scripting language : create new elements, access and modify existing elements, create graphics, animations, send Ajax requests, etc.

Test Brython online

If you want to test Brython online you can visit the following:

Gallery of examples

There is a gallery of examples where you can see simple and advanced examples using vanilla Brython or interacting with Javascript libraries.

Documentation

You can start by reading the official Brython tutorial.

Full documentation is available on the official site. You can read the docs in English and French.

Curious about how Brython works ?

A tutorial explains how to build Android applications with Brython.

Community (questions, feedback, issues, new features, ...)

You can subscribe and post to the mailing list.

If you find a bug/issue or do you want to see a new feature in Brython, please, open a new issue.

If you want to contribute to Brython, please read the contributing guide.

Thank you

  • BrowserStack for providing an access to their online testing environment.

NPM DownloadsLast 30 Days