7 ms·
Pythonium: Python 3 to JavaScript translator
- deleted 13y ago[deleted]
- vonseel 13y agoHow would one go about writing an HTTP call in Python and converting to appropriate XMLHttpRequest in JavaScript? Is this project capable of something like that, and using the native JavaScript ability, rather than a predefined jQuery variable? Trying to understand what this would be used for besides basic object mapping, iterating, etc.
- shantanubala 13y agoXMLHttpRequest is a facility provided by the browser - when you are making a request, you are just accessing objects and functions provided by the host environment. In Python, the "host environment" is generally CPython with the standard library. In JavaScript, the "host environment" is typically the browser with the DOM. This is a project that translates code in Python syntax to JavaScript, so you would use XMLHttpRequest just as you would in JavaScript, but with Python syntax.
- knappador 13y agoSo if I get the names perfect, I can use the js API's provided by the browser and only need to compile to test my code? Next. No...not next. Please no more next. Make it stop. Just stop. Please. There are so many projects that can use your assistance. I can give you ten open issues off the top of my head that I would like fixed. So many useful, interesting, thought-provoking programs are just dying to be written.
- bulatb 13y agoIf they haven't already, they could hide the DOM and nasty host-provided objects behind something more Pythonic. As long as it transpiles to XMLHttpRequests, it doesn't matter what the Python looks like.
- knappador 13y agoAnd I'll build a mock environment so that I can run my python script as if there is a real DOM to manipulate, and then I'll write a test rendering system to actually display the page as if everything is running in Python. Eventually I'll just implement every single aspect of a web browser so that I can be sure that after I stop transpilfering Python, I can indeed just start putting <script type="text/python"> everywhere and convince everyone to use it, so my giant mock environment will become the latest, greatest browser, and Google will spend millions adding a tune-able JIT to cpython, leading someone to decide what a great idea it would be to use this JIT as the runtime for server-side Python, and Pode.py will take over the web, and "isomorphic" Python frameworks will sprout faster than TCP connections on a DDOS'd server. And after that, we can start writing and re-writing everything to "transpile" to PHP and use hip-hop to make it C++. I can't spend any more attention on random me-to JS without feeling like I'm waiting for my vestigial appendix to fall out.
- amirouche 13y agoIn pythonium access to JavaScript objects is transparent look at the example app.py of the webgl demo: https://github.com/pythonium/pythonium.github.io/blob/master/js/app.py#L68 https://github.com/pythonium/pythonium.github.io/blob/master...
- mmgutz 13y agoIs there an online transpiler to compare the Python source to the generated JavaScript as on the CoffeeScript site?
- amirouche 13y agoYou can compare the Javascript code generated by the Veloce mode [0] and by compliant mode [1] [0] https://gist.github.com/amirouche/7768738 https://gist.github.com/amirouche/7768738 [1] https://gist.github.com/amirouche/7768858 https://gist.github.com/amirouche/7768858
- wting 13y agoThere's also a quick demo of Pythonium here: http://pythonium.github.io/ http://pythonium.github.io/ The generated JS: http://pythonium.github.io/js/app.py.js http://pythonium.github.io/js/app.py.js
- julenx 13y agoLet's pollute the global namespace!
- amirouche 13y agoYou can use requirejs but integration isn't perfect for now.
- e12e 13y agoBut the python source isn't available? The project seems rather light on example code for now...
- ma2rten 13y agoI found it: https://github.com/pythonium/pythonium.github.io/blob/master/js/app.py https://github.com/pythonium/pythonium.github.io/blob/master...
- myers 13y agoHere is the python source https://github.com/pythonium/pythonium.github.io/blob/master/js/app.py https://github.com/pythonium/pythonium.github.io/blob/master...
- analog31 13y agoThis should help me learn JavaScript!
- ma2rten 13y agoI don't think that is good idea. Javascript has it's own idioms and also gotchas. You are not going to learn them by looking at translated python code. Read JavaScript The Good Parts instead.
- analog31 13y agoUpon further thought, I agree. I wouldn't have wanted to learn Python from a Visual Basic to Python translator. ;-)
- acjohnson55 13y agoAnd Javascript Allongé, my personal favorite! https://leanpub.com/javascript-allonge/read https://leanpub.com/javascript-allonge/read
- rdtsc 13y agoAlso Pyjamas: http://pyjs.org/ http://pyjs.org/ Skulpt: http://www.skulpt.org/ http://www.skulpt.org/ and another one I forgot. They are all are cool projects. They diligently solve 73.56% of the problem of Python to Javascript translation, and the other 26.44% remains a very hard task. This is not unlike many other problems in programming (well, even in real life). So it is not for the lack of knowledge, experience or hard work, it is just a very hard problem.
- paulftw 13y agoAnother one is http://brython.info/ http://brython.info/
- rdtsc 13y agoYes, that's the one. Thank you.
- nine_k 13y agoPyjamas is much more that a py-js translator. And their demos seem broken here and there.
- Demiurge 13y agoI tried to use pyjamas and it was just almost impossible... It seems to be strictly for GWT (widgets, rpc) style sites. No easy way to build some small python written library or integrate with other pure JS libraries from what I could tell.
- solox3 13y agoI wonder how multiple inheritance is handled by this webdev holy grail. I know CoffeeScript had some issues handling its certain attributes* within its constructors, so it'll be interesting to see how pythonium does the same and beyond. * forgot what
- acjohnson55 13y agoI hope your object model doesn't require understanding the MRO to trace the code, unless you're doing some serious library plumbing that I can't even really fathom using in front-end code right now. But I'm fairly certain you could mimic those semantics with wrappers around prototypal inheritance somehow.
- amirouche 13y agoIt's not possible to use prototypal inheritance to mock even simple inheritance since “method = object.metod; method();” loose the reference to “this” as “object”, it's a gotcha in the lower level mode Veloce of Pythonium that I tried to explain in https://github.com/pythonium/pythonium/wiki/Pythonium-Veloce-Cookbook#methods-as-callbacks https://github.com/pythonium/pythonium/wiki/Pythonium-Veloce.... The MRO and thus multiple inhertiance is implemented in pythonium_create_class https://github.com/pythonium/pythonium/blob/master/pythonium/lib/runtime.py#L114 https://github.com/pythonium/pythonium/blob/master/pythonium...
- acjohnson55 13y agoYeah, that's why I phrased it "wrappers around". Check out https://github.com/lukasolson/Backbone-Super https://github.com/lukasolson/Backbone-Super for example, which wraps and replaces Backbone.extend.
- amirouche 13y agoI never mentionned super. Anyway, thanks for the link.
- 13y ago
- leoh 13y agoThese kinds of things are cute and fun—but does anyone use these sorts of language x to language y translators in practice? Not talking about specialized language interpretation. With JavaScript, the most common operation is to manipulate and capture from the DOM—something that this tool is not capable of readily doing, not, at least, without getting one's hands dirty with JS.
- sumukh1 13y agoThe challenge is to do that is that there is no "Pythonic" way of manipulating the DOM. When we were building a similar translator we included a way to interact with JQuery through a js object (i.e. js.document.addEventListener) -- but it still required the user to think about and understand javascript.
- amirouche 13y agoIt's still required to understand web APIs it's different from understanding JavaScript
- Paradigma11 13y agoI do think https://github.com/clojure/clojurescript https://github.com/clojure/clojurescript is quite popular. I have personally used http://www.websharper.com/home http://www.websharper.com/home for a few projects quite successfuly.
- terhechte 13y agoI think ClojureScript is actively being used in practice. I think most others are more or less neat experiments.
- chubot 13y agoPretty sure Light Table is built in ClojureScript (on top of Node-Webkit), which is pretty impressive!
- ilaksh 13y agoIf you like Python you should really look at CoffeeScript and even better ToffeeScript. You can write code that looks a lot like Python. Actually the spirit of Python is to be close to pseudo code, and if you want to you can write CoffeeScript/ToffeeScript that beats Python in that respect.
- acjohnson55 13y agoI really like CoffeeScript, but I don't think it's really closer to pseudocode than Python. Python, for example, eschews anonymous functions in favor of forcing you to name things. It also tries to avoid terse symbols for language features in favor of verbosity. To me, CoffeeScript syntax is somewhere between Python and Ruby.
- kayoone 13y agooh so ToffeeScript is the latest fashion now ? See, thats why i dont get into any of those alternative JS syntaxes.
- chc 13y agoIf you refrain from any area where one thing might be more popular than another, you're going to have a very hard time getting anything done.
- kayoone 13y agoDid i say that? I just prefer pure javascript the way its meant to be for reasons of consistence with coworkers, documentation and reading other peoples code. That and i simply dont find JS to be all that bad.
- porker 13y agoI do find the number of options hard to choose from. The hacker approach seems to be to learn a bit of everything (and enjoy it) but for people like me where programming is a necessary tool to get shit done, trying to decide between these 10 options and pick one that will turn out to be the best/still be useful in 3 years is hard.
- daemonk 13y agoAnonymous functions?
- cookiecaper 13y agoI'd prefer a real solution to the JavaScript problem: put a real language in a web browser. JavaScript has done very well for what it is, but even Brendan Eich openly admits it was a rushed project and was never intended to be the One True Client-Side Programming Stack. We have lots of people writing very complex programs meant to alleviate some of the problems with JavaScript, when that effort could reasonably go into making Python a first-class web language by including a VM in browser distributions and writing bindings that allow DOM manipulation.
- icebraining 13y agoWe have a VM in browser distributions, it's called JavaScript.
- MrBra 13y agoAnd no one is complaining about it.
- agumonkey 13y agoFrom the few that I've seen ES6 will bring order to the universe.
- adamtj 13y agoIt's called asm.js. Make your real language compile to that. asm.js not Javascript. It's an assembly languange (well, at least an IR) for a simple static virtual machine. It's easy to write intepreters and JITs for that virtual machine, and modern browsers are starting to do so. It's not much of a stretch to think of asm.js as a way for browsers to run native code. Asm.js is the clever hack that lets you shoehorn any language you want into the browser and to run it fast.
- Jon_E 13y ago"I'd prefer a real solution to the JavaScript problem: put a real language in a web browser." That's what motivated Google to create Dart, which just went 1.0