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

Mouse events
Keyboard events
Focus events
Drag events

Query string

Using Javascript objects and libraries

Brython-specific built-in modules




Working with Brython

Options of function brython()
Testing and debugging
Deploying an application


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

module browser.aio

This module supports asynchronous programming in Brython, using the keywords async and await.

It replaces the asyncio module in CPython standard library, which cannot work in the browser context:

  1. it uses blocking functions such as run() or run_until_complete(), and the way browsers work make it impossible to define functions whose execution is suspended until an event occurs.

  2. the browser has its own implicit event loop, it is not possible to define another one as the asyncio modules does with the function set_event_loop().

The module browser.aio defines the following asynchronous functions:

Ajax requests

ajax(method, url[, format="text", headers=None, data=None, cache=False])

req = await ajax("GET", url) inside an asynchronous function gives back control to the main program, and resumes the function when the Ajax request of the type method ("GET", "POST", "PUT", etc.) to the specified URL is completed. The return value is an instance of the class Request (see below).

format is the expected response format. It can be one of:

"text" : the response is a string

"binary" : an instance of class bytes

"dataURL" : a string formatted as dataURL

headers is a dictionary with the HTTP headers to send with the request.

data is a string or a dictionary that will be sent with the request to form the query string for a "GET" request, or the request body for "POST".

cache is a boolean indicating if the browser cache should be used

get(url[, format="text", headers=None, data=None, cache=False])

shortcut for ajax("GET", url...)

post(url[, format="text", headers=None, data=None])

shortcut for ajax("POST", url...)

Request instances

Instances of class Request, as returned by await ajax(), await get() or await post(), have the following attributes:

data : the response body, with the format defined by argument format

response_headers : a dictionary matching the response headers

status : HTTP response status as an integer (200, 404...)

statusText : HTTP response status as a string ("200 Ok", "404 File not found"...)

Other asynchronous functions

event(element, name)

evt = await aio.event(element, "click") suspends execution of an asynchronous function until the user clicks on the specified element. The return value is an instance of the DOMEvent class (cf. section events)


In an asynchronous function, await sleep(n) gives back control to the main program and resumes function execution after n seconds.

Running an asynchronous function


Runs a coroutine, ie the result of a call to an asynchronous function defined by async def. This is a non blocking function: it doesn't wait until the asynchronous function is completed to execute the instructions in the following lines. The time when the next instructions are run is not (easily) predictable.


ao implements a Future class which is inspired by the class of the same name in asyncio. This allows you to convert functions using callbacks to asynchronous functions that can be used with the keyword await.

The version of Brython is basic, it currently implements only the methods set_result and set_exception. Please refer to [the asyncio documentation]( for details.


Entering text in an INPUT element (customised input() function)

from browser import alert, document, html, aio

async def main():
    input = html.INPUT()
    document <= input
    while True:
        ev = await aio.event(input, "blur")
            v = int(
            alert(f"Value: {v}")
        except ValueError:
            input.value = ""

Reading files asynchronously

from browser import document, html, aio

async def main():
    # Text file
    req = await aio.ajax("GET", "test.html")
    # Binary file
    req = await aio.get("memo.pdf", format="binary")
    # Read binary file as dataURL
    req = await aio.get("eraser.png", format="dataURL")
    # display the image in an IMG tag
    document <= html.IMG(

Use a function with callbacks

from browser import timer, aio

async def main():
    fut = aio.Future()
    timer.set_timeout(lambda: fut.set_result("timeout!"), 2000)
    result = await fut
    print(f"future awaited, result: {result}")