Brython, browser-based Python

And if like a new lover or a Taylor Swiftie or a vegan you need your favorite programming language to really be everywhere, there’s Brython, which allows you to put Python code in <script> tags, where it talks to the DOM in a mostly Pythonic way, and have that code translated on the fly into JavaScript and sent to the browser.

Awkward in conception, probably pointless in production, it’s straight forward in execution. Import brython.js, call brython() on load, and you can do stuff like this

from browser import document, alert
def echo(ev):
    alert("Hello {} !".format(document["zone"].value))

document["test"].bind("click", echo)

The gallery shows some more useful things, like doing Ajax requests in Brython, sorting tabular data

But then I don’t know,…In the richer examples, like ones with decorators binding Brython functions to events, it starts to look a little more like a framework, like Flask or something. 

@bind("#get_test", "click")
def get(ev):
    ajax.get("/cgi-bin/get_test.py",
             oncomplete=show,
             data={"foo": 34})

Since Taylor Swift does country tunes, pop, politics, and emo, maybe it is time to throw out all my other music :-)

   

Leave a Reply

Your email address will not be published. Required fields are marked *