Brython (Browser Python) is an implementation of Python 3 running in the browser, with an interface to the DOM elements and events.
Here is a simple example of an HTML page running Python:
<html>
<head>
<script type="text/javascript" src="/path/to/brython.js"></script>
</head>
<body onload="brython()">
<script type="text/python">
from browser import document, alert
def echo(event):
alert(document["zone"].value)
document['mybutton'].bind('click', echo)
</script>
<input id="zone"><button id="mybutton">click !</button>
</body>
</html>
To use Brython, all there is to do is:
- Load the script brython.js.
- Run the function
brython()
on page load, like<body onload=brython()>
. - Write Python code inside tags
<script type="text/python">
or linking it.
Brython supports most of the syntax of Python 3, including comprehensions, generators, metaclasses, imports, etc. and many modules of the CPython distribution.
It includes libraries to interact with DOM elements and events, and with existing Javascript libraries such as jQuery, 3D, Highcharts, Raphael etc. It supports lastest specs of HTML5/CSS3, and can use CSS Frameworks like Bootstrap3, LESS, SASS etc.
The most simple way to get started, without anything to install, is to use the distribution available online through rawgit. You can either choose the latest stable release, for instance for release 3.2.9 :
<script type="text/javascript"
src="https://cdn.rawgit.com/brython-dev/brython/3.2.9/www/src/brython.js">
</script>
or the current development version :
<script type="text/javascript"
src="https://cdn.rawgit.com/brython-dev/brython/master/www/src/brython.js">
</script>
The previous code will allow you to use raw python code, but if you import modules from the standard library this will be slow as they are implemented using ajax calls. If you want to test your projects using a standalone javascript file with the available stdlib included you should use:
<script type="text/javascript"
src="https://cdn.rawgit.com/brython-dev/brython/3.2.9/www/src/brython_dist.js">
</script>
or the current development version :
<script type="text/javascript"
src="https://cdn.rawgit.com/brython-dev/brython/master/www/src/brython_dist.js">
</script>
To install Brython locally, if you have a CPython distribution with pip
:
pip install brython
then create a new directory and run
python -m brython --install
or by loading the latest version of the Brython zip file from the releases page.
In both cases, the distribution includes brython.js (the core Brython engine) and brython_stdlib.js (a bundle of all the files in the standard distribution).
It also includes the page demo.html that shows a few examples of how you can interact with a web page using Python as the scripting language : create new elements, access and modify existing elements, create graphics, animations, send Ajax requests, etc.
If you want to test Brython online you can visit the following:
There is a gallery of examples where you can see simple and advanced examples using vanilla Brython or interacting with other javascript libraries.
Documentation is available on the official site. You can read the docs in English, French and Spanish.
The most updated docs usually are the English and French versions so if you want to be up-to-date, please, use these versions.
Curious about how Brython works ?
There is a main mailing list in English. Also, you can find mail list in other languages but the activity is very low and it is recommended to use the main one in English.
If you find a bug/issue or do you want to see a new feature in Brython, please, open a new issue.
There's a Brython Community on Google Plus.
IRC: #brython on freenode
-
Brython KanBan of Tickets Whats Brython Team working on ?
-
Throughput Graph: Statistics about our work...
- BrowserStack for providing an access to their online testing environment.