
A Python 3 implementation for client-side web programming - spicavigo
http://www.brython.info/
======
cmbaus
I can't believe all the negative comments here. Someone has implemented Python
in JavaScript. I personally think that is incredible.

~~~
lowboy
I think it's the hubris that's putting people off:

> Brython is designed to replace Javascript as the scripting language for the
> Web

~~~
MatthewPhillips
That's a silly complaint. Doesn't every language designer feel that their
product is better than the thing it desires to replace?

~~~
readme
A better question: Is python really any _better_ than JavaScript? Sure, it has
advantages here and there. But there are things I like about JavaScript that
are better than python. For example, having anonymous functions that are more
than a single expression inside a lambda.

Yes, I know you can declare a function inside another and you get a closure.
But, JavaScript's syntax is just nicer.

~~~
kyrias
Obligatory <https://www.destroyallsoftware.com/talks/wat>

~~~
jMyles
Wow. I had never seen that. There are literally tears streaming down my face I
was laughing so hard.

~~~
sesqu
I enjoyed it more this time than I did when I last saw it, perhaps because I
was more familiar with the showcased peculiarities last time (the theme is
garbage in, garbage out).

------
jashkenas
For the curious, here's _eight_ other fun implementations of "Python in
JavaScript" to explore:

[https://github.com/jashkenas/coffee-script/wiki/List-of-
lang...](https://github.com/jashkenas/coffee-script/wiki/List-of-languages-
that-compile-to-JS#python)

~~~
javis
One not mentioned in that list is Empythoned, which is the actual CPython
engine compiled to JS using Emscripten.

Repo: <https://github.com/replit/empythoned>

Live demo: <http://repl.it/languages/Python>

------
toddmorey
The way they position Brython is pretty off-putting and kind of confuses the
issue.

CoffeeScript page: "CoffeeScript is a little language that compiles into
JavaScript."

Brython page: "Brython is designed to replace Javascript as the scripting
language for the Web."

Maybe that's the end goal, but the reality is far from it. Of course JS is
still required, and of course the Python gets compiled into JS.

Finally, the approach seems a bit off. I would think it would be better for
the Python to be compiled with a pre-processor, not done on the client.

~~~
tinco
The approach and the phrasing seems exactly right to me and not at all off-
putting.

The goal of Brython is to replace javascript on the web, seems straightforward
right? The way they go about it is to implement a compiler in javascript, and
use that to bootstrap a python in the browser community. Seems like that is
much easier than convincing Chrome/Firefox/IE to include a python compiler in
their browser.

How would Brython replace javascript if it were precompiled on the server?

~~~
redblacktree
Why not provide both options, ala LESS?

~~~
alexboots
Because that takes time and effort and this is relatively new.

~~~
redblacktree
tinco seemed to be arguing that allowing server-side compilation would be
harmful to the goals of the project. That's orthogonal to your argument about
time and effort.

------
rdtsc
Skulpt, PyJS and this, all good and fun experiment but all do the first 60%
then they linger around. I wish they all started talking to each other and
just got one single project to 90%.

Have any of these been used in production with any success. It seems like PyJS
had most traction?

------
idoco
IMHO, This comment page is an interesting example to how unreasonably
emotional programmers can be about their technology.

It looks as though, some developers are almost insulted by the idea, that
someone tried to replace a language that is so commonly accepted, and that
they have worked so hard to master.

This also happens in many other fields of science/technology, but I'm always
surprised to see it among programmers who are considered very practical
people.

~~~
manojlds
If Chrome, Firefox and maybe even IE came out with support for python as a
client side scripting language, the very same people here on this comment
thread would happily jump on it and try to use it. The point is not about
replacing one with another, but HOW it is done.

------
rachelbythebay
It's too bad it didn't replace it on this page. Without JavaScript, this page
is blank.

~~~
brudgers
It's not the first time I had to turn on JavaScript to view a page. On the
other hand, it is the first time that I have ever done so and discovered a
totally logical reason for having to do so.

I got something of value by tweeking NoScript - knowledge, not just ads. Then
again, I'm a yokel.

~~~
anonymouz
I tried it and didn't get much value out of it: What appeared was mostly just
JavaScript that should have been there all along, had the page not been
broken. That animated clock? Yeah, well, I can do without it.

------
iandanforth
I love python and this is awesome. But I _HATE_ magic global objects. 'ctx'
seems to be at the heart of everything but is never explicitly passed into the
scope or imported.

This should really be fixed.

Also "from html import *"? No no no. Never, not even once.

~~~
Stratoscope
The ctx global and 'from html import *' are just part of the demo code. Look
at line 289 of the HTML file where ctx is initialized. It's not some magic
thing baked into Brython, it's just sample code. Of course you could write
either of these in any way you prefer.

~~~
jzwinck
A demo of a new way to use a programming language shouldn't be full of these
"Oh, that crap is just for the demo, it would look better in practice." It's a
programming language demo, show us something that makes us want to use it.

~~~
Stratoscope
I agree completely: sample/demo code should be the best you can make it. And
I'm sure the Brython authors would welcome a pull request that improves their
demo code.

But the comment I replied to was a bit misleading (no doubt unintentionally):
it made two quite valid complaints about coding practices, without mentioning
that these are only in the demo code.

When I read that comment I thought, "WTF? Brython injects a magic 'ctx' global
into your Python code?" It doesn't, so I thought it was worth pointing out
that these are just sample code issues that wouldn't affect any code _you_
write in Brython.

------
md224
I do like the idea, but it inevitably suffers from the same flaw that afflicts
all other compile-to-js libraries: making sense of errors will still require
knowledge of how the underlying javascript works.

Perhaps a development plugin for Chrome would allow debugging in python?

~~~
tuxracer
Source maps should resolve this issue

~~~
md224
Good point! I ashamed to say I hadn't thought much about how source maps will
pave the way for anything-to-javascript web programming... very cool.

------
ggordan
Previous discussion: <https://news.ycombinator.com/item?id=5133047>

~~~
dbaupp
Also, <https://news.ycombinator.com/item?id=4923530>

------
bayesianhorse
While I would like to see Python in the browser, this isn't it. I would still
prefer coffeescript or dart over brython. Both languages aren't that hard to
learn, and brython is lacking many features which would make Python more
pleasant to deal with...

However, because of performance reasons, the very features that make python
unique, especially the type system, are more javascript-like than python-like.

What I would like to see is a Python-implementation which is mostly feature
complete, along the lines of emscripten, but still has direct access to the
dom or other javascript objects.

------
mcintyre1994
It's a really nice demo, and I really like the idea so far (although
precompiling to JS would be awesome), but I really don't see Python taking
over Javascript as the scripting language.

It's way nicer in my opinion, but I think its fussy white space is likely to
be an issue for this use case. All the large Javascript libraries get minified
etc, and Python really isn't open to that sort of stuff. It's a great language
to use normally, but I don't see it replacing Javascript. I'd much rather
write in Python and compile to Javascript though.

------
azakai
Link to console demo from that page is broken, sadly.

edit: looks like if you remove "_en" then you get to the right place,
<http://www.brython.info/tests/console.html>

Looks like pyjs, in that it has python syntax but JavaScript semantics, for
example numbers turn into doubles here (but should be arbitrary-precision ints
in Python).

~~~
e12e
Well, python 2.7:

    
    
        >>> a = 1/3
        >>> type(a)
        int
    

python 3.2

    
    
        >>> a = 1/3
        >>> type(a)
        <class 'float'>
    

brython:

    
    
        print(type(1/3))
        <class 'float'>

~~~
azakai
Correct, that's a python 2/3 difference. But both 2 and 2 have arbitrary
precision integers AFAIK, try this code

    
    
        x = 1
        for i in range(100):
            x = x*2
            print(i, x)

~~~
e12e
Indeed. Just to verify that it's not just a difference in
(string)representation, something like:

    
    
        print(9**32-(9**32-5))
    

Also works (brython returns 0, python2&3 return 5)

------
marcamillion
I love this. I wish someone would do this for Ruby.

Not CoffeeScript - I want to be able to write Ruby that manipulates DOM
objects.

~~~
jashkenas
Here's _eight_ options for you. Enjoy: [https://github.com/jashkenas/coffee-
script/wiki/List-of-lang...](https://github.com/jashkenas/coffee-
script/wiki/List-of-languages-that-compile-to-JS#ruby)

~~~
MrBra
Last time i checked it, it contained links to old/dead projects, which seems
still the case. Anyway AFAIK there is no such a similar thing to Brython for
ruby, and by that I mean a ruby implementation specifically written in JS and
optimized to deal with DOM and browser environment in general (if anybody
knows about something like that or is working on it please inform me). It'd be
such a great thing to have!

~~~
adambeynon
opal is very much still alive: <http://opalrb.org>

Most of the work in the recent weeks has been improving the ruby <=> js bridge
so that calling js methods/functions from ruby is possible.

------
anuraj
As such, it is theoretically possible to support any scripting language on
client side environment. I hope browsers will start shipping with multiple
scripting engines in the future. JavaScript is not the best scripting language
out there.

------
spoiler
I don't mean to rain on anyone's parade, but how come opalrb hasn't gotten
this much attention on HN (although it is a bit different from this)?

There seems to be this _gah a fucking ruby developer/thing, again_ motif going
on around HN (was hurtful at first) that makes it harder to discover cool and
new ruby things!

P.S: although Python is nit my goto language, I still find this brilliant!

~~~
tinco
Opalrb has been on the HN frontpage. Why is there always someone whining about
their perception of HN user sentiments lately? There's even a post whining
about the user sentiments in the comments of _this_ post on the frontpage now.

------
jferge
Interesting, but it is little more than a novelty.

------
aniketpant
I don't get the point why should I use Python to run Javascript.

JS has been in use around the web for a long time now and I don't see the need
why should I go about using Python for the very same? Doesn't one language has
a purpose of it's own?

If it wasn't the case, then people would have worked on creating a unified
language for all our needs.

~~~
icebraining
Nobody says you should, it's purely a personal preference :)

 _Doesn't one language has a purpose of it's own?_

Usually no, most domains are served by more than one language, otherwise we
would all be using Perl to write server-side web applications.

While it's debatable whether JavaScript is particularly well suited for
client-side web code, the fact is that having only one language of choice is
mostly an historical accident.

------
jameswburke
Why the hate on JavaScript? I know it's not the most elegant language in the
world, but do we really need this? Instead of a program "x" run in "y", how
about we just expand our horizons a bit and learn another language with
different paradigms?

Python is great for some stuff, but let JS do what it does.

~~~
gingerlime
I don't hate javascript, and there are good arguments to learn new languages.
Absolutely. But it's a bit of a my-way-or-the-highway with client-side web.

I wish I could have a _choice_ of the language to run in the browser, and
still do all the things javascript can. Having some kind of a bytecode virtual
machine on the browser, that many different languages can compile to, might be
the best solution.

an interesting rant from Zed Shaw on this <http://vimeo.com/43380467>

~~~
jameswburke
The issue with web (although this could be expanded to apply to _most_
languages) is where it's used/interpreted. Browsers aren't a quickly changing
environment (haha, IE 6). It takes a few years to adopt the latest and
greatest. Shit, it took nearly a decade for us to see native curved corners
(border-radius).

Again, not that I don't agree, it's just hard to stay _up to date_ with the
greatest while still trying to support outdated tech like html tables.

The W3C is the 'authority', but is really at the hands of the major browser
manufacturers. Implementing an interpreted version of Python sounds great, but
is it worth the time and effort? Will it increase my productivity or
compliance compared to JavaScript? As of now, nope.

I'm happy writing bastardized JS (not that my code is bastardized, rather
that's just the language [took way too long to understand == vs ===]) for the
web right now. Every day new tools and frameworks are developed to make it
easier, and I just don't see the advantage and overhead just to write a weird
version of python that compiles into JS.

------
MatthewPhillips
Most disappointing part of most -> js languages is that they adopt the async
nature of JavaScript, thus making them pretty dissimilar from their native
environments. Is it that difficult to rewrite a sync workflow to an async one
by the interpreter?

~~~
amirouche
You mean async to sync workflow, this is possible with generators
[https://developer.mozilla.org/en-
US/docs/Web/JavaScript/New_...](https://developer.mozilla.org/en-
US/docs/Web/JavaScript/New_in_JavaScript/1.7) it similar to what is done in
gevent. BUT, such sync code for async is just style, the code is still async
you just write it like it sync code. In this sens it can be understood as a
feature of the language but generators are meant for more than that.

------
sengstrom
If you want to X to replace Y, shouldn't first order of business be letting me
know why?

------
leephillips
Very impressive work!

Issues with the examples:

I got nobody walking on the 3d walker (Chrome on Ubuntu); also it was
psychedelic fun clicking into negative territory on the pie chart.

~~~
bwang29
I also enjoyed the negative territory over there : )

------
tudorconstantin
Is it possible to minify and obfuscate python code?

~~~
pyre
Python 'compiles' to .pyc files, but I'm not show how much that compresses
them, or if it's possible to reconstruct the source from it.

~~~
mh-
.pyc files are _very_ reversible.

there are open source projects that decompile them to runnable code, the
original symbol names are even intact - this means you don't lose the original
naming of variables, functions, arguments, etc.

~~~
comex
Doesn't stop you from obfuscating Python separately just like any other
language, though.

------
aniketpant
Being a front-end developer myself, I find such an idea a complete waste for
people like me. We have put in a lot our time understanding how JS functions
and most of the interactive interfaces you come across use jQuery. If Brython
were to become a standard, then would you be writing a new JS library that
would allow me work with DOM faster and in a better fashion?

------
xtc
Why not both?

------
vinitool76
What is even the need? And it needs JS to work. Not so convincing demo!

~~~
CaveTech
You do realize that it's a python interpreter written in Javascript?

No, browsers cannot magically run python now.

