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
 

module javascript

The module javascript allows interaction with the objects defined in Javascript programs and libraries present in the same page as the Brython program.

javascript.import_js(url[, alias])
Imports the Javascript module at specified url and adds alias to the local namespace.

If alias is missing, the module name is computed from the url; for instance, for import_js('js_test.js') the alias is js_test.

The module must define the name $module, an objet whose attributes make the namespace of the imported module.

Example: if Javascript script at js_test.js is

var $module = {
    x: 1
}

then a Python script can use it this way:

import javascript

javascript.import_js("js_test.js", alias="js_module")
assert js_module.x == 1

javascript.import_modules(refs, callback)
imports the Javascript modules whose urls are in the list refs. When they have all been imported, function callback is called with the module as arguments. callback() thus takes as many arguments as there are urls in refs.

For instance if module exporter.js is

const x = 1

export {x}

it can be used in a Python script with

import javascript

def main(module):
    assert module.x == 1

javascript.import_modules(
    ['/path/to/exporter.js'],
    main)

See in the gallery a use case with the modules of three.js.

javascript.JSObject
The class of Javascript objects used in a Python program. If a Javascript program defines an object with

window.jsobj = {x: 1}

it can be tested in a Python program with

from browser import window
import javascript

assert isinstance(window.jsobj, javascript.JSObject)

javascript.py2js(src)
Returns the Javascript code generated by Brython from the Python source code src.

javascript.this()
Returns the Brython object matching the value of the Javascript object this. It may be useful when using Javascript frameworks, eg when a callback function uses the value of this.

The module also allows using objects defined by the Javascript language. Please refer to the documentation of these objects.

javascript.Date doc
Constructor of date / time objects.

from javascript import Date

date = Date.new(2012, 6, 10)
print(date.toDateString())

javascript.JSON doc
Object to convert to and from JSON objects. It exposes two functions:

stringify: serialize simple objects (dictionaries, lists, tuples, integers, reals, strings)

parse: conversion of a JSON-formatted string into a simple object

javascript.Math doc
Object for mathematical functions and constants.

javascript.NULL
the Javascript object null. Can be used to test if a Javascript object is null.

javascript.Number doc
Constructor for objects of type "number".

javascript.RegExp doc
Constructor of "regular expression" objects, using the Javascript-specific syntax, which doesn't fully match that of Python. The method exec() of instances of this class can be applied to Python strings:

from javascript import RegExp

re = RegExp.new(r"^test(\d+)$")
print(re.exec("test44"))

javascript.String doc
Constructor of Javascript objects of type "string". Must be used to call methods that accept Javascript regular expressions as parameters:

from javascript import RegExp, String

re = RegExp.new(r"^test(\d+)$")
print(String.new("test33").search(re))

javascript.UNDEFINED
the Javascript object undefined. Can be used to test if a Javascript object is undefined.