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
 

Accessing elements

Getting access to an element can be done in different ways. The most usual is to use its identifier, ie its attribute id : with an input field defined by

<input id="data">

we can get a reference to this field by

from browser import document
data = document["data"]

document is defined in module browser and refers to the HTML document. It behaves like a dictionary whose keys are the identifiers of the elements in the page. If no element has the specified id, the program raises a KeyError exception.

All the elements in the page have a method get() that can be used to search elements:

  • elt.get(name=N) returns a list of all the elements descending from elt whose attribute name is equal to N
  • elt.get(selector=S) returns a list with all the elements descending from elt whose CSS selector matches S

elt.select(S) is an alias for elt.get(selector=S). A few examples :

document.select('.foo')       # elements with class "foo"
document.select('form')       # list of elements "<form>"
document.select('H1.bar')     # H1 elements with class "bar"
document.select('#container') # the element with id "container", same as
                              # [document["container"]]
document.select('a[title]')   # A elements with an attribute "title"
document.select('#tid td')    # the TD elements inside the element with id #tid

To select a single element, use select_one() instead of select(). If the selector matches several elements, select_one() returns the first one.

See the MDN documentation for more information and examples of CSS selectors.