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
 

Problem

Create an HTML table

Solution

In this example and the following, we will only show the Python script ; the surrounding HTML code will remain the same as in the previous examples

To create a table, we use the HTML tags : TABLE (the table),TR (a table row), TH (a header cell) and TD (a cell)

The table is made of rows, each row is made of cells ; the first row is generally made of "header cells" describing the value in the matching column

Here is a simple example :

from browser import document
from browser.html import TABLE, TR, TH, TD
table = TABLE()
row = TR() # create a row
# add header cells
row <= TH("Country")
row <= TH("Capital city")
table <= row # add the row to the table

# add a row
row = TR()
row <= TD("United States")+TD("Washington")
table <= row

# erase initial content
document['zone'].clear()

# insert table in the element
document['zone'] <= table

Initial content

We can build a table from a list of lists :

from browser import document
from browser.html import TABLE, TR, TH, TD

lines = [ ['Morrissey','vocals'],
    ['Johnny Marr','guitar'],
    ['Mike Joyce','the drums'],
    ['Andy Rourke','the bass guitar']
    ]
t = TABLE()
for line in lines:
    t <= TR(TD(line[0])+TD(line[1]))
document['zone1'].text = ''
document['zone1']<= t

Initial content