Introduction

Installation

Limitations of the "file" protocol

Frequently asked questions

Syntax, keywords and built-in functions

Standard distribution

import implementation

Brython packages

Browser interface

Introduction - DOM API
Creating a document
Accessing elements
Attributes, properties and methods

Events
Mouse events
Keyboard events
Focus events
Drag events

Query string

Interactions with Javascript

Brython-specific built-in modules

browser
browser.aio
browser.ajax
browser.html
browser.local_storage
browser.markdown
browser.object_storage
browser.session_storage
browser.svg
browser.template
browser.timer
browser.webcomponent
browser.websocket
browser.worker

Widgets
browser.widgets.dialog
browser.widgets.menu

interpreter
javascript

Working with Brython

Execution options
Testing and debugging
Deploying an application

Cookbook

Hello world !
Insert content in an element
HTML markup (bold,italic...)
HTML table
Bind and unbind events
Handle options in a SELECT
Drag and drop
Get the content of an element
Read the content of a file
Store objects locally
Example of onmouseover
 

No installation

You can use Brython without having to install anything, just by including the Brython scripts from a CDN:

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

The minor version can be specified with brython@3.13 and the micro version with brython@3.13.0.

Brython is also available with cdnjs:

<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.13.0/brython.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.13.0/brython_stdlib.min.js">
</script>

Local installation

To install Brython locally:

  • if your PC has CPython and pip, install the package brython by

    pip install brython

The package installs a client program brython-cli (same as python -m brython).

then in an empty directory run

brython-cli install

  • if you can't use this method, go to the releases page on Github, select the latest version, download and unzip Brython-x.y.z.zip.

In both cases the directory has the following files :

  • brython.js : the Brython engine, to include in the HTML page
  • brython_stdlib.js : groups all the modules and packages of the part of the Python standard library supported by Brython
  • demo.html : a page with a few examples of how to use Brython for client-side development

brython.js includes very often used modules : browser, browser.html, javascript.

If your application uses modules of the standard distribution, you need to include brython_stdlib.js besides brython.js:

<script type="text/javascript" src="brython.js"></script>
<script type="text/javascript" src="brython_stdlib.js"></script>

Updates

When a new version of Brython is published, the update is done by the usual command:

pip install brython --upgrade

In the application directory, you can then update the Brython files (brython.js and brython_stdlib.js) by:

brython-cli update

Installing a CPython package

A CPython package installed by pip can be installed in a Brython application by the command add_package <package name>.

For example:

pip install attrs
brython-cli add_package attrs

Important warning : Brython executes programs written in Python, but not those written in C. This means that for instance numpy and the data science packages that use it will not work.

Additionnaly, the ability of browsers to send HTTP request is limited for security reasons. Packages such as requests, or urllib.request in the standard distribution, will not be able to send arbitrary requests to any url. This is common to all the implementations of Python in the browser.

Other commands

make_modules

creates an application-specific distribution, to replace brython_stdlib.js by a smaller file. See section import.

make_dist

generate a CPython package, suitable for distribution by PyPI, to install a Brython application. See section Deploying a Brython application

make_package

generates a "Brython package", allowing to distribute a module or a package in a very straightforward way. See section Packages Brython

-- version

prints the Brython version

Web server

The HTML files can be opened in the browser, but it is recommended to start a web server in the application directory.

The most straightforward is to use the module http.server in CPython standard distribution:

python -m http.server

The default port is 8000. To choose another port:

python -m http.server 8001

You can then access the pages by entering http://localhost:8001/demo.html in the browser address bar.