10 ms·
A Python 3 implementation for client-side web programming
- iandanforth 13y agoI 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.
- deleted 13y ago[deleted]
- Stratoscope 13y agoThe 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 13y agoA 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 13y agoI 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.
- johncoltrane 13y ago"ctx" is a very common name for the "context" with which you work when doing stuff in <canvas>. There's nothing magical about it. Also it's defined at the bottom of the script which, I admit, is a bit weird: if hasattr(canvas,'getContext'): ctx = canvas.getContext("2d") from html import * looks bad, I agree, but I believe they did that to allow us to play with their toy from the console.
- ggordan 13y agoPrevious discussion: https://news.ycombinator.com/item?id=5133047 https://news.ycombinator.com/item?id=5133047
- dbaupp 13y agoAlso, https://news.ycombinator.com/item?id=4923530 https://news.ycombinator.com/item?id=4923530
- xtc 13y agoWhy not both?
- rdtsc 13y agoSkulpt, 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?
- azakai 13y agoLink 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 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 13y agoWell, 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 13y agoCorrect, 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 13y agoIndeed. 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)
- tudorconstantin 13y agoIs it possible to minify and obfuscate python code?
- pyre 13y agoPython '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- 13y ago.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 13y agoDoesn't stop you from obfuscating Python separately just like any other language, though.
- sargun 13y agoThey're fairly reversable out of the box. There are some modifications to cx-freeze which allow you to bundle your code in a way that makes it more difficult to reverse. What are you shipping that you need source code security with?
- rachelbythebay 13y agoIt's too bad it didn't replace it on this page. Without JavaScript, this page is blank.
- SquareWheel 13y agoVirtually every browser has Javascript support today. Heck, even Googlebot is executing some Javascript these days.
- darkarmani 13y agoNoscript allows you to turn the Javascript off. Requiring javascript to see any content at all is a little absurd.
- staunch 13y agoJavascript is a prerequisite of the modern web. Refusing to accept that is absurd.
- wdyjs 13y ago>Javascript is a prerequisite of the modern web. Citation needed.
- galenko 13y agoJavaScript is like the electricity of the web. If you disable it, it's like walking around the real world with an "emp field", sure some things will still work, but it's kind of dumb to expect the modern technologies to work without their main food source. The technologies have evolved... Why are you trying to keep us in the stone age? Or why do you expect backwards compatibility?
- camus 13y agothe http protocol is the electricity of the web , not javascript. Javascript is more like the home automation of the web and you sure can live without it. IT is stupid to offer 0 content when javascript is turned off. Some people might check the content on tablets,phones or devices that dont have javascript turned on by default.
- md224 13y agoI 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 13y agoSource maps should resolve this issue
- md224 13y agoGood 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.
- amirouche 13y agoEvery exception in brython has original line source, you don't need source maps. The thing is that you can't type Python in the console, yet.
- tantalor 13y agoJust a matter of implementing a Chrome extension to extend the developer tools with the compiler.
- vinitool76 13y agoWhat is even the need? And it needs JS to work. Not so convincing demo!
- CaveTech 13y agoYou do realize that it's a python interpreter written in Javascript? No, browsers cannot magically run python now.
- sengstrom 13y agoIf you want to X to replace Y, shouldn't first order of business be letting me know why?
- toddmorey 13y agoThe 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.
- brudgers 13y agoThe argument for compiling the Python with the pre-processor seems roughly analogous to traditional arguments for compiling rather than interpreting languages - i.e. how many times the conversion from source code to machine instructions must be done. One isn't necessarily better than the other - sometimes human generated python source code in the browser may be more useful than machine generated javascript. If nothing else, it allows us to explore a new way of constructing web apps and determine whether it is better.
- gingerlime 13y agoI'm not sure what the commenter meant, and perhaps a silly comment to make, but doesn't python's strong indentation make it harder to simply include on a webpage? proxies, ad filters, and other optimizers might break the lines in unexpected places. I guess it can be solved by gziping the file, or base64 encoding it. But this is also an argument (albeit trivial or weak) for compiling it on the server rather than the client.
- tinco 13y agoThe 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?
- jferge 13y agoInteresting, but it is little more than a novelty.
- leephillips 13y agoVery 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 13y agoI also enjoyed the negative territory over there : )
- cmbaus 13y agoI can't believe all the negative comments here. Someone has implemented Python in JavaScript. I personally think that is incredible.
- lowboy 13y agoI think it's the hubris that's putting people off: > Brython is designed to replace Javascript as the scripting language for the Web
- MatthewPhillips 13y agoThat's a silly complaint. Doesn't every language designer feel that their product is better than the thing it desires to replace?
- readme 13y agoA 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.
- deleted 13y ago[deleted]
- MatthewPhillips 13y agoMost 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?
- anarchy8 13y agoThat's because an synchronous client-side scripting language would be useless for the web.
- amirouche 13y agoYou mean async to sync workflow, this is possible with generators https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7 https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_... 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.
- aniketpant 13y agoI 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 13y agoNobody 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.
- idoco 13y agoIMHO, 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.
- notimetorelax 13y ago> programmers who are considered very practical people I don't think it is universally true. Myself I know how much I can be taken away by a new technology or knowledge.
- manojlds 13y agoIf 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.
- redblacktree 13y ago> programmers who are considered very practical people. Ever met an Enterprise Architect?
- jashkenas 13y agoFor the curious, here's eight other fun implementations of "Python in JavaScript" to explore: https://github.com/jashkenas/coffee-script/wiki/List-of-languages-that-compile-to-JS#python https://github.com/jashkenas/coffee-script/wiki/List-of-lang...
- javis 13y agoOne not mentioned in that list is Empythoned, which is the actual CPython engine compiled to JS using Emscripten. Repo: https://github.com/replit/empythoned https://github.com/replit/empythoned Live demo: http://repl.it/languages/Python http://repl.it/languages/Python
- marcamillion 13y agoI love this. I wish someone would do this for Ruby. Not CoffeeScript - I want to be able to write Ruby that manipulates DOM objects.
- amirouche 13y agoYou can do it while following a similar approach to what is done for PythonScript, it's easy. PythonScript is compiled to Javascript, it use the Python abstract syntax tree module to parse python code then generate javascript. The subset of python that is (directly) translated to Javascript is PythonJS. With PythonJS is implemented some runtime stuff and higher level construct of Python. http://apppyjs.appspot.com/ http://apppyjs.appspot.com/ Sorry the editor is broken.
- jashkenas 13y agoHere's eight options for you. Enjoy: https://github.com/jashkenas/coffee-script/wiki/List-of-languages-that-compile-to-JS#ruby https://github.com/jashkenas/coffee-script/wiki/List-of-lang...
- MrBra 13y agoLast 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 13y agoopal is very much still alive: http://opalrb.org 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.
- marcamillion 13y agoThanks. Btw, I have nothing against Coffeescript per se. I just prefer Ruby :)
- jameswburke 13y agoWhy 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 13y agoI 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 http://vimeo.com/43380467
- jameswburke 13y agoThe 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.
- aniketpant 13y agoBeing 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?
- bayesianhorse 13y agoWhile 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.
- spoiler 13y agoI 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 13y agoOpalrb 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.
- anuraj 13y agoAs 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.
- mcintyre1994 13y agoIt'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.