CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-brython

Python-to-JavaScript transpiler that enables Python 3 development in web browsers with full DOM integration and standard library support

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

index.mddocs/

Brython

Brython (Browser Python) is a Python-to-JavaScript transpiler that enables Python 3 development directly in web browsers. It provides a complete Python 3 runtime environment with full DOM integration, event handling, and access to browser APIs, eliminating the need for server-side Python execution for client-side web application logic.

Package Information

  • Package Name: brython
  • Package Type: PyPI (Python) and npm (JavaScript)
  • Language: Python (runtime in JavaScript)
  • Installation: pip install brython
  • Browser CDN: https://cdn.jsdelivr.net/npm/brython@3.13.2/brython.min.js

Core Imports

Browser-side Python imports:

from browser import document, window, console, alert
from browser.html import DIV, P, A, INPUT, BUTTON

CLI usage:

import brython
# Access via brython-cli command-line tool

Basic Usage

HTML Setup:

<!DOCTYPE html>
<html>
<head>
    <script src="https://cdn.jsdelivr.net/npm/brython@3.13.2/brython.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/brython@3.13.2/brython_stdlib.js"></script>
</head>
<body onload="brython({debug: 1})">
    <div id="output"></div>
    <button id="click-me">Click Me</button>
    
    <script type="text/python">
    from browser import document, bind
    from browser.html import P
    
    # DOM manipulation
    document["output"] <= P("Hello from Python!")
    
    # Event handling
    @bind(document["click-me"], 'click')
    def handle_click(event):
        document["output"] <= P("Button clicked!")
    </script>
</body>
</html>

Local Development Setup:

# Install Brython CLI
pip install brython

# Initialize new project
mkdir my-brython-app
cd my-brython-app
brython-cli install

# Start development server
brython-cli start_server 8000

Architecture

Brython consists of several key components:

  • CLI Tools: Project initialization, package management, and development server
  • Runtime Engine: JavaScript-based Python 3 interpreter running in browsers
  • Browser Module: Python interface to DOM, events, and browser APIs
  • Standard Library: Browser-compatible versions of Python standard modules
  • UI Framework: Widget-based GUI toolkit for web applications

This architecture enables full-stack Python development where client-side logic runs as Python in the browser while maintaining compatibility with existing JavaScript libraries and browser APIs.

Capabilities

CLI Development Tools

Command-line interface for managing Brython projects, installing packages, creating distributions, and running development servers.

# Main CLI commands accessed via brython-cli
def install(install_dir=".", no_demo=False): ...
def update(update_dir="."): ...
def add_package(package, dest_dir=None): ...
def make_package(package_name, src_dir=".", exclude_dirs=None, output_path=None): ...
def make_modules(output_path=None, reset=False, modules_paths=None): ...
def start_server(port=8000, bind="localhost"): ...

CLI Tools

Browser Runtime Engine

JavaScript-based Python interpreter that executes Python 3 code in web browsers with full language support.

function brython(options?: BrythonOptions): void;
function $B.runPythonSource(src: string, options?: RunOptions): any;
function $B.py2js(src: string, module: string, locals_id?: string, parent_scope?: any): string;

Runtime Engine

DOM and Browser Integration

Python interface to HTML DOM elements, events, and browser APIs with Pythonic syntax and full browser functionality access.

# Core browser objects
browser.document: DOMDocument
browser.window: BrowserWindow  
browser.console: Console

# Event binding decorator
def bind(element: Element, event: str, options: dict = None) -> Callable: ...

# Template engine for dynamic HTML
class Template:
    def __init__(self, element: Element | str, callbacks: list = None): ...
    def render(self, **kwargs) -> None: ...

Browser Integration

HTML Element Creation

Python classes for all HTML5 elements with attribute management, styling, and event handling capabilities.

class DIV(Element): ...
class P(Element): ...
class A(Element): ...
class BUTTON(Element): ...
class INPUT(Element): ...
class FORM(Element): ...
class TABLE(Element): ...
# ... all HTML5 elements available

HTML Elements

AJAX and Networking

HTTP request handling with Python-friendly interfaces for web service communication and data exchange.

class Ajax:
    def __init__(self): ...
    def bind(self, event: str, callback: Callable) -> Ajax: ...
    def open(self, method: str, url: str, async_: bool = True) -> None: ...
    def send(self, data: Any = None) -> None: ...

AJAX and Networking

Storage and Data Persistence

Browser storage APIs including localStorage, sessionStorage, and IndexedDB with Python interfaces.

class LocalStorage:
    def __getitem__(self, key: str) -> str: ...
    def __setitem__(self, key: str, value: str) -> None: ...
    def __delitem__(self, key: str) -> None: ...

class IndexedDB:
    def __init__(self, name: str, version: int = 1): ...
    def open(self) -> None: ...
    def transaction(self, stores: list, mode: str = 'readonly') -> Transaction: ...

Storage

Timers and Animation

Timer functions and animation frame requests for time-based functionality and smooth animations.

def set_timeout(func: Callable, interval: int, *args) -> int: ...
def set_interval(func: Callable, interval: int, *args) -> int: ...
def clear_timeout(timer_id: int) -> None: ...
def clear_interval(timer_id: int) -> None: ...
def request_animation_frame(func: Callable) -> int: ...

Timers and Animation

UI Framework

Widget-based GUI toolkit with layout management, styling, and event handling for creating desktop-like web applications.

class Widget:
    def __init__(self, parent: Widget = None): ...
    
class Button(Widget):
    def __init__(self, text: str = "", parent: Widget = None): ...
    
class Entry(Widget):
    def __init__(self, value: str = "", parent: Widget = None): ...
    
class Dialog(Widget):
    def __init__(self, title: str = "", parent: Widget = None): ...

UI Framework

WebSocket Communication

Real-time bidirectional communication with WebSocket support for live data and interactive applications.

class WebSocket:
    def __init__(self, url: str, protocols: list = None): ...
    def send(self, data: str | bytes) -> None: ...
    def close(self, code: int = 1000, reason: str = "") -> None: ...

WebSocket

Types

# Configuration types
class BrythonOptions:
    debug: int  # Debug level 0-3
    cache: bool  # Enable module caching
    indexeddb: bool  # Use IndexedDB for caching
    pythonpath: list[str]  # Additional module paths
    ids: list[str]  # Specific script IDs to run
    args: list[str]  # Command line arguments

# DOM element base
class Element:
    def __init__(self, content: str = "", **attrs): ...
    def bind(self, event: str, callback: Callable) -> Element: ...
    def unbind(self, event: str, callback: Callable = None) -> Element: ...
    
# Event handling
class Event:
    target: Element
    type: str
    preventDefault: Callable
    stopPropagation: Callable

# Browser objects
class DOMDocument:
    def __getitem__(self, id: str) -> Element: ...
    def createElement(self, tag: str) -> Element: ...
    def createTextNode(self, text: str) -> TextNode: ...

class BrowserWindow:
    location: Location
    history: History
    localStorage: Storage
    sessionStorage: Storage

Install with Tessl CLI

npx tessl i tessl/pypi-brython

docs

ajax-networking.md

browser-integration.md

cli-tools.md

html-elements.md

index.md

runtime-engine.md

storage.md

timers-animation.md

ui-framework.md

websocket.md

tile.json