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". 

 

Brython

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.

 Brythonlogo

More Information

Brython

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.

 

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

 

Banner


Eclipse JKube 1.16 Goes GA
08/04/2024

Eclipse JKube makes deploying your Java application to a Kubernetes cluster a breeze. Let's find out what's new.



Interact With Virtual Historic Computers
14/04/2024

Alan Turing's ACE computer is a legendary computer that is particularly special for I Programmer - our account of it was the first ever history article on the site when it launched in 2009. Now this i [ ... ]


More News

 

Last Updated ( Tuesday, 07 May 2013 )