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
 

Execution options

A number of options are available to customize script execution. They can be defined at the HTML page level, or by script.

Defining options

Declaring options for the whole page is done through the attributes of a specific HTML tag <brython-options>. This tag should be placed preferably in section <body> of the document, not in <head>.

For instance, to define the options debug and cache:

<brython-options debug="1" cache="true">
</brython-options>

To define an option for a specific script, use the attribute of the <script> tag:

<script type="text/python" debug="2">

Options only available at page level

ids

by default, all the scripts in the page are executed. This options specifies a list of scripts to execute, as a space-separated list of identifiers (attribute id of the <script> tag)

If the string is empty, no script is executed.

<brython-options ids="scriptA scriptB"></brython-options>

indexedDB

specifies if the program can use the indexedDB database to store a compiled version of the modules located in brython_stdlib.js or brython_modules.js. Defaults to true.

Options available at page or script level

An option defined at script level (attribute of <script>) has precedence over the same option defined at page level (attribute of <brython-options>).

args

equivalent to command-line arguments, available in programs by sys.argv. Values are strings.

cache

if set to true, the Ajax calls to import modules, load external scripts by <script src="foo.py"> or read files with open() use the browser cache. Defaults to false.

debug : the debug mode

  • 0 (default) : no debugging. Use this when the application is debugged, it slightly speeds up execution
  • 1 : error messages are printed in the browser console (or to the output stream specified by sys.stderr)
  • 2 : the translation of Python code into Javascript code is printed in the console
  • 10 : the translation of Python code and of the imported modules is printed in the console

pythonpath

a space-separated list of paths where imported modules should be searched

static_stdlib_import

boolean, indicates if, in order to import modules or packages from the standard library, the static mapping table in the script stdlib_paths.js should be used. Defaults to true

The function brython(options)

In Brython versions prior to 3.12, options could only be defined for all the scripts in the page as an argument to the function brython(), explicitely called on page load by the syntax

<body onload="brython({debug: 2})">

For backwards compatibility, this syntaxe remains usable in version 3.12.

If a page defined a tag <brython-options>, the values passed to function brython() replace those of the tag.