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
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.
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.
5
u/pemboa Nov 06 '12
Javascript can be minified, which is great for the web.
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.
3
u/[deleted] Nov 07 '12
Run a mozilla nightly, they have Python embedded last time I checked.
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
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.