Brython, Python in the browser
brython.info
brython.info
Another approach that may work in Chrome is to use Chrome Native Client to embed a 'proper' Python runtime.
I've often lamented that we have Javascript and not Python :( http://williamedwardscoder.tumblr.com/post/20771096806/why-d...
I write lots of Javascript; mostly 'raw' Javascript in the webGL space. Here's just of the top of my head what's wrong with Javascript:
* looping over elements has too much boilerplate
* the mindfulnesss of 'this' is distracting
* the duality of dictionaries and lists and how that's an easy lie to fall for is fustrating
* that there are multiple frameworks trying to use $
On the other hand, their anonymous functions are very nice. And, I rather like prototypes vs classes. A bit. Sometimes
With Python, I like significant whitespace (so sue me) and especially the syntax for collections and packing/unpacking them.
I've blogged along these lines a lot in the past e.g. http://williamedwardscoder.tumblr.com/post/18319031919/progr...
By this I refer to that in Python, most syntactic constructs map to more primitive operations that are accessible (and overridable) in the language itself. For example, anything can be made callable, indexable, sliceable, iterable, etc., by providing the right interface. It is like operator overloading on steroids. Even attribute access (the dot operator) can be overridden in extremely powerful ways. This is great for library writers, who can use it to provide the simplest possible API to their users.
I also like very much Python's data model. Things consistently behave as references (even primitive values which all behave as immutable singletons, although of course they are not implemented as such). It just somehow fits and makes sense once you have the model in your mind. JavaScript is slightly less intuitive in this way, and while it makes sense once you understand it, there are a few caveats you need to keep in mind (variable hoisting, scoping, anticipating different context objects). Prototype inheritance works fine but requires a special mind-set, and to me it seems JS fits better for functional programming than object-oriented Java-style.
Python's shortcomings are mainly syntactical (not counting the shortcomings of CPython's implementation specifically), such as not having an easy syntax for function expressions. This is the main reason functional programming can get slightly awkward in Python.
To be honest I'm not intimate enough with JS to give a big list of my biggest language-level annoyances off the top of my head. It's just a general distaste/annoyance that occurs every time I get involved in JavaScript code, either the reading or the writing of it. Python, on the other hand, is nearly readable by non-programmers, and is otherwise super cool. I recognize that others may have a different opinion on this than I have and while they're not necessarily wrong for themselves, I believe Python is more accessible in general.
I think the facts that a) almost no one writes bare in-browser JavaScript anymore, it's all through jQuery and b) CoffeeScript and other persistent "rewrite/simplify/improve JS" movements exist and keep coming up are ample evidence that the basic tools provided by the browser and the JS language are insufficient.
Minification should not be considered as a major reason to forgo the benefits of alternate scripting languages.
This has the advantage over NaCl that it works in all browsers, not just Chrome. It's also portable.
Otherwise, as someone whose first and second strongest languages are Python and Javascript, I agree with you. JavaScript is a beautiful language that accommodates a variety of programming styles. As you mention, JavaScript is also much more performant than Python, which makes me questions whether Python would ever be a viable replacement for JavaScript in the browser. Add to that JavaScript's excellent webworker API compared to Python's choices for parallel programming.
Now, Lua, on the other hand, would be an excellent browser language (specifically the LuaJit implementation). Lua is close enough to JavaScript that folks could easily pick it up, and its blazing fast. But it will never happen.
Besides, there are so many great JS libraries now that in most cases, I wouldn't want to switch to Lua in the browser and face re-inventing the wheel (same reason why I generally use Python instead of Lua elsewhere). But its fun to think about.
I've never thought about that but could make sense.
> as well as its enforced whitespace, which makes it easier to write maintainable code.
This is something I've never understood and where I tend to disagree. When starting with "whitespace" languages like Coffee, Ruby or Python people initially are please with clean and tidy looking code but in the long run this code is not maintainable since all the white space makes the code looking not distinctively enough. In contrast, bracket based languages like C or JS give much better orientation. I can quickly see where code blocks start or I can immediately recognise functions etc.
> Lua, on the other hand, would be an excellent browser language
Totally agree
Where do you get this? The only instance where I see Python becoming unreadable due to whitespace is when a developer abuses whitespace where it's not needed e.g. a = [ 'a', 'list' ]
Another example could be chaining method calls Java-style, but all of this is alleviated by just following PEP8.
The significance of whitespace has no effect on the readability of a language.
This is not true. Some Javascript _implementations_ are definitely faster than some Python _implementations_, but nothing about either language makes one inherently faster than the other.
And in the Brython case, the Python is being translated to Javascript, so it is getting the same benefits out of a fast, JIT-compiled implementation that Javascript normally gets.
You can't make such a statement confidently unless you examine the specs of both languages.
For example, Python integers promote to bigints automatically while JS numbers don't. For one more, in Python you can overload attribute access, while in JS you can't. Both of these are language features rather than implementation details, and both potentially affect performance. There are probably others.
Indeed it does. Its one of the hardest modern language I have ever tried to teach someone. All the minor trip-wires (like http://stackoverflow.com/questions/1995113/strangest-languag...), but the worst part is that there are always at least 5 ways to do something.
Out of those 5, two is bad and should never be done but you would be lucky if any book mention this fact. Of the three left, only two is ever considered as good through noone can agree which one is the preferred way. One solution ends up being neither hated or praised enough to be worth mentioning in a book or ranting article.
So to figure out which ones to avoid, and which ones to use, one can either use what ever superstitious choice one learned by a previous teacher, or read massive amount of javascript articles and books to get a "feel" for it (no guarantee through). When you try to teach a student all the different methods, they will get information overload so one is simply forced to avoid large part of the language and hope they act like "don't think, just do what I say".
Python in the browser would just be a clear improvement over javascript.
I personally would choose any other modern language over javascript.
ANY.
I've learnt it and used it 8 or 9 years now, to me it just sucks. And still no-one knows how to write it well, no-one agrees how to structure it and every 2 years there is a whole new way to write javascript that comes out. I'm beginning to suspect it's because there is no good way to write it.
Quite the opposite. The more you learn about Javascript, the more you realize how it's a giant hack.
> Neither, JS' syntax is bad or ugly.
Languages are more than syntax. The problem is the excess of boilerplate, lack of good APIs and general lack of consistency.
Tell me a language where this is acceptable: https://gist.github.com/4471029
=> Underscore, 4KB in production and gives you _.sortBy and _.difference among other amenities
The basic gist of the criticism seems to be the code is basically just directly translating a Python token list (not an AST) into Javascript, and as such is reasonably fragile and horrible code (the main translator function: https://code.google.com/p/brython/source/browse/trunk/py2js.... ).
Skulpt is much better, and about as incomplete.
def set_clock():
ctx.beginPath()
ctx.fillStyle = "#FFF"
ctx.arc(width/2,height/2,ray*0.89,0,2*math.pi)
ctx.fill()
show_hours()
This is not the case with some other languages that compile to JavaScript, e.g. LiveScript allows dash-case almost everywhere.btw: it's not a decorator :) but a camelCase->under_score renamer it's pretty easy to write
import re
def unCamel(obj, *maybe_camels):
if not maybe_camels:
maybe_camels = filter(lambda n: not n.startswith('_'), dir(obj))
for maybe_camel in maybe_camels:
underscore = re.sub(r'([^A-Z])([A-Z])([^A-Z])',
lambda m: '%c_%c%c' % (m.group(1), m.group(2).lower(), m.group(3)),
maybe_camel)
if underscore != maybe_camel:
setattr(obj, underscore, getattr(obj, maybe_camel))
if __name__ == '__main__':
class CamelCase(object):
def multiWordMethod(self):
print('called multiWordMethod')
def multiWordFunction():
print('called multiWordFunction')
unCamel(CamelCase) # magic
import sys
unCamel(sys.modules[__name__], 'multiWordFunction') # magic
CamelCase().multi_word_method()
multi_word_function()
[0] http://www.python.org/dev/peps/pep-0008/threading.Thread would seem to be consistent with the Google Python Style Guide (threading = module, Thread = Class)
http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht...
Providing the camelCase will allow whole swathes of JS code to be cut-n-pasted; that code would be legal Python as well as JS.
I could see myself using this for a SaaS app I am developing right now; would be useful to run the same language on the client as on the backend. But it needs to performant in this case.
I'm amazed at how this has slipped past me so far. Does anyone know the specifics on how it works (performance, does it implement the complete language, etc)?
And if it's run by a true Python engine, does the user have to have Python installed do run it? (Hard for me to tell since all my machines ATM have Python installed).
Also see: http://ajaxian.com/archives/pquery-php-and-jquery | https://metacpan.org/release/pQuery
Do you have some evidence for this, or are you just judging it by the fact it is written in JavaScript? I can imagine source-to-source translation between Python to JS that would provide an excellent implementation of Python.