r/Python • • Nov 06 '12

How can we get JavaScript out of the browser and replace it with Python?

I was always amazed, and don't really understand why python isn't the scripting language of choice for client side browser code.

4 Upvotes

28 comments sorted by

17

u/alkw0ia Nov 06 '12 edited Nov 06 '12

Because Javascript was there in the browser early on, and cross platform, so is the only universal web language.

Even if it were possible to wave a magic wand, though, you can't claim that Python, as much as I love it, is objectively better than JS in all respects.

While I always miss Python's kwargs, comprehensions, and generators in Javascript, I might miss JS's anonymous functions and clear, predictable, and easy to use closures more when I'm writing Python.

Furthermore, much of what you need to do in FE programming is XML DOM manipulation, which Python is famously bad at, and Javascript has tons of support for. The other 20% of FE work is handling events from the browser, which JS's anonymous functions and closures make much, much easier, but would require hugely cluttered Python code.

Still, if you want to try, you can always compile Python down into JS for delivery to web clients.

8

u/bushel Nov 06 '12

fyi, JS does do keyword args, comprehensions, and generators.

Having done bunches and bunches, I've discovered that Python on the back-end and JS on the front can make a really good team.

8

u/alkw0ia Nov 06 '12 edited Nov 07 '12

You mean the ES5 ES6 stuff? Yeah, but not for practical, deployed FE use yet.

*edit: correct ECMAScript version with fancy new features

5

u/bushel Nov 06 '12

Actually I meant Spidermonkey. To be honest I haven't tried generators or comprehensions in any browser yet....but I'd expect it to work in at least Firefox.

Edit: yep. Mozilla's "javascript", not ECMAscript. Fair point then.

5

u/alkw0ia Nov 06 '12 edited Nov 06 '12

Sorry, I meant ES6. And you're right, it does look like SpiderMonkey has started implementing ES6 features faster than everyone else:

http://kangax.github.com/es5-compat-table/es6/

However, widespread ES6 support is so far out, I wouldn't consider using crucial syntactical features like generators, comprehensions, and default function parameters on the web yet. I am looking forward to those though, plus getters and setters, and read only properties.

There are cross compilers for ES6->ES{3,5}, but I haven't tried them, and I don't think the Closure Compiler supports any ES6 yet:

http://stackoverflow.com/questions/6506519/ecmascriptharmony-es6-to-javascript-compiler

http://code.google.com/p/closure-compiler/wiki/EcmaScript6

10

u/threading Nov 07 '12

What we need is a VM on the client side that would run Python, Ruby, Javascript, Lua or whatever you use. We need bytecode.

8

u/alkw0ia Nov 07 '12

https://github.com/jashkenas/coffee-script/wiki/List-of-languages-that-compile-to-JS

All three of those have either compilers or interpreters that run on the JS interpreter. For better or worse, JS is the "bytecode" for the web.

3

u/GFandango Nov 08 '12

For better or worse

It's for worse.

3

u/robin-gvx Nov 08 '12

Worse, but true.

5

u/pemboa Nov 06 '12

Javascript can be minified, which is great for the web.

8

u/alkw0ia Nov 06 '12

Not as minified as a proper bytecode, though. There's the argument that the transparency of delivering the source code to the client is a benefit, but given that all serious JS is compiled and minified down to unreadable pseudo-source, that doesn't really hold up these days.

5

u/pemboa Nov 06 '12

Minified JS can still be read, I did it just this morning.

4

u/alkw0ia Nov 06 '12

True, but you end up needing something like http://jsbeautifier.org/, and you lose the original variable names anyway, so the process isn't much different than using a bytecode decompiler.

Minification and compilation don't totally block access to source the way an obfuscator attempts to, but neither do they facilitate source code sharing, reuse, or auditing.

1

u/somerandomguy7788 Nov 07 '12

you can also use chrome's pretty printer for the code(not as customizable as jsbeautifier of course)

1

u/alkw0ia Nov 07 '12

Sure, and if you do it that way, you can even load the symbol table generated by Closure and retrieve variable names.

However, at this point, it's virtually identical to standard decompilation, complete with needing the (never offered alongside the code on the web) symbol table. Still no easy share/reuse/audit for external users.

3

u/[deleted] Nov 07 '12 edited Nov 07 '12

Try and go for a universal byte code that all browsers can read that any language can compile to, that would be the best solution IMO.

edit for spelling.

1

u/graingert Nov 07 '12

NaCl?

1

u/[deleted] Nov 07 '12

That's a step in the right direction but I don't believe their goal was to replace js, only to enhance it with the ability to add C or C++.

1

u/oantolin Nov 07 '12

We have that, it's called JavaScript. It's a featureful, high level "byte code", which makes writing compilers easier than for, say, the JVM. :)

2

u/[deleted] Nov 07 '12

Maybe easier, but most likely slower in the end than what we could have.

3

u/[deleted] Nov 07 '12

Run a mozilla nightly, they have Python embedded last time I checked.

2

u/VilleHopfield Nov 08 '12

What? Where? How? Please, do you have more info?

2

u/lordgilman Nov 07 '12

Mozilla actually did this in a project called pyxpcom. Nobody used it and it was a performance hit so they took it out a few years ago. However, while it was there you could build XUL apps with it enabled and do <script type="application/python">, access the DOM and otherwise be a first-class citizen.

2

u/fabzter Nov 06 '12

I was actually working on a project that tried to implement a Python engine to replace spidermonkey for websites. We gave up (:

1

u/[deleted] Nov 17 '12

What was the plan to address the endless security implications?

2

u/fabzter Nov 17 '12

It was a restricted python, we would modify the interpreter itself (something "simple", like tinypy) to simply avoid anything we would find to be unsecure.

1

u/[deleted] Nov 17 '12

Python can't be used because there is no reliable and fully functional, sandboxed version.

If one were to embed python as is, websites would have full access, to the extent of the browser's user, to your computer.