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.

 

espbook

 

Comments




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

 

Banner


Wasmer 5 Adds iOS Support
12/11/2024

The Wasmer team has released Wasmer 5.0. The WebAssembly runtime adds experimental support for more back ends including V8, Wasmi and WAMR. It also now has iOS support, and upgraded compilers includin [ ... ]



Lightbend Announces Akka 3
15/11/2024

Lightbend, the company that developed Akka, has announced Akka 3, and has changed its name to Akka. The company produces cloud-native microservices frameworks, and Akka is used for building distribute [ ... ]


More News

 

Last Updated ( Tuesday, 07 May 2013 )