DEV Community

petercour
petercour

Posted on

Python in the browser

You can use Python on the web, instead of Javascript.
Yes, really.

There's a project named Brython: "A Python 3 implementation for client-side web programming".

So how does it work:

Step 1: Include Brython

<script type="text/javascript" src="/src/brython.js"></script>

Step 2: Write your code, say

<body onload="brython(1)">

<script type="text/python">
from browser import document, alert

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

document["test"].bind("click", echo)
</script>
<p>Your name is : <input id="zone" autocomplete="off">
<button id="test">clic !</button>
</body>

Step 3: Demo https://brython.info/gallery/hello.html

More resources,

Learn python and demos

Latest comments (9)

Collapse
 
mdor profile image
Marco Antonio Dominguez

Has this any benefit? I mean, it seems like this adds a new complexity layer to any project without any benefit, everything which is a wrapper or non native become slower and non maintainable in a long term.
Up to now this just push a personal preference without even consider the disadvantages of the tech debt it will generate.

Collapse
 
genta profile image
Fabio Russo • Edited

JS is a magic language for browser/DOM/frontend stuff... it's useless to use another language (made with other kind of stuff in mind).

But, hey, It looks cool anyway.

Collapse
 
cardiox12 profile image
Cardiox12

In theory that's very cool, but I tested it for an extension project where I needed python,
that was very painful to use for the moment, because brython doesn't support some functions related to DOM, etc.
So I hope this will get better and better.
Cool module tho. :)

Collapse
 
sebastiannielsen profile image
Sebastian-Nielsen

But why?
What are the advantages?

Collapse
 
garador profile image
Garador

I personally find it appealing since I'm learning Python. Wouldn't take it to production, tough...

Collapse
 
lesha profile image
lesha 🟨⬛️

it's fun

Collapse
 
sebastiannielsen profile image
Sebastian-Nielsen

I mean, sure, I do like writing in Python more than JavaScript ... but other than that, what advantages are there?

Thread Thread
 
lesha profile image
lesha 🟨⬛️ • Edited

Mostly syntactic sugar i guess. I'd love to be able to use list/dict comprehensions, named arguments, but most of all, format strings.

Collapse
 
dowenb profile image
Ben Dowen

Mind, blown. Thank you.