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

Read the content of a file

Solution

We use the built-in function open() to load the file content

import time
from browser import document

fake_qs = '?foo=%s' %time.time()
document['zone'].value = open('file.txt'+fake_qs).read()

Note the query string with a random value at the end of the file name : it is required to refresh the result if the source file is changed between two calls

The next example adds a timeout function to print a message in case the file was not found after 4 seconds :

import time
from browser import ajax, document 

def on_complete(req):
    if req.status==200 or req.status==0:
        document["zone"].value = req.text
    else:
        document["zone"].value = "error "+req.text

def err_msg():
    document["zone"].text = "server didn't reply after %s seconds" %timeout

timeout = 4

def go(url):
    req = ajax.Ajax()
    req.bind("complete", on_complete)
    req.set_timeout(timeout,err_msg)
    req.open('GET',url,True)
    req.send()

go('file.txt?foo=%s' %time.time())