Brython - Python In The Browser
Written by Alex Armstrong   
Tuesday, 07 May 2013

Brython is a Python to JavaScript compiler, but it does the whole job in the browser and so makes Python appear to be a client scripting language. 

Brythonic is an ancient Celtic language, but in this case Brython stands for Browser Python, or I suppose it could be reference to the Life of Brian. Wherever the name derives, Brython is another of the growing examples of treating JavaScript as an assembly language. In this case, though, the approach is slightly different. The idea is that you can write Python in the browser as if it was a language with built-in browser support. 

For example:

<script type="text/python">
 def echo():
    alert("Hello %s !" %doc["zone"].value) </script>

You can see that this looks like a native script. You can also see that it is Python with a few extensions to enable it to work in the browser environment - doc represents the DOM and you can use it to select an element using indexing doc["zone"] is the element with id "zone". 



Analog Clock In Python


So how does this work?

How can you add a new language to any browser?

The answer is very simple. The Brython library has to be loaded for it to work and you have to include a call to the bryrthon() function before the script starts executing - usually something like: 

<body onload="brython()">

What this does is to locate Brython script tags and replace them with JavaScript script tags containing the equivalent JavaScript code. Yes that right, brython compiles the Brython code into JavaScript as the page is loaded. It's a clever method and its only real cost is the time it takes to compile. If this is unacceptable then you can opt to compile the code "offline" and insert the equivalent JavaScript into the page. 

Of course the big disadvantage of compiling anything to JavaScript is that direct debugging has to be done in terms of JavaScript rather than the original language.

You might be skeptical of the likely success of a compiler that converts Python to JavaScript but the cores of the two languages are very similar. They are both untyped, they both have associative arrays as their fundamental datatypes and they both implement dynamic objects.

While Bython isn't a 100% implementation of Python - for one thing it lacks most of the libraries which Python programmers come to rely on - it does support a good subset of the language including generators, comprehensions, classes, decorators and so on. It also has enough additions to make programming in the browser possible - DOM, SVG, Ajax and access to JavaScript objects. 

The project is at an early stage of development but if you want to lend a hand then visit the website.


More Information


Related Articles

Arrays in Python

Advanced Python Arrays - Introducing NumPy

CoffeeScript Supports Literate Programming

JavaScript Assembly Language



To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.





or email your comment to:



The Pi 5 Breaks The Mold

The Pi 5 is an exciting development bringing even more power for not much additional cost, but it also signals a bigger change in the Pi line-up, obscured by secretiveness.

Othello Solved - It's A Draw!

The board game Othello, also known as Reversi as it played by reversing the colors of black and white disks, has been solved using alpha-beta pruning. Given "perfect play", that is a situation in [ ... ]

More News


Last Updated ( Tuesday, 07 May 2013 )