On Thu, Jun 14, 2012 at 5:33 PM, Joe Ryan <[email protected]> wrote: > Hi,
hello Joe, > Hope I'm mailing the right place. I'm hoping to use pyjs in a project of > mine but there's still some questions I have that I'm a bit confused about. yeah apologies on the pyjs-devel list -- i will have to make it more obvious that it's not a discussion list (mainly commits and automated stuff) > First, to give you an idea of where I'm coming from and what my needs are: > I'm working on an open source webgame engine for Python in Django. The > engine is designed for tile-based games (games like civ, dungeon crawl stone > soup, or puzzles), with an emphasis on allowing multiplayer play. A key > feature of the engine is that it will do automatic lag compensation by > automagically mirroring the game command code (a file with a bunch of python > functions) and database locally (django models; the models interface is > handled seperately but any custom methods need to be translated) on the > client side, and then syncing with the server via ajax. In both cases, the > functions in question consist mostly of fetching data from the database, > manipulating it, and storing it back. The "fetching" and "storing" parts are > handled separately; our concern here is only with the manipulation part. neat :-) > Our current attempt to do this uses the empythoned project > (https://github.com/replit/empythoned) to run python in the browser, which > is actually Cpython compiled into javascript by emscripten and run in a web > worker. This works really great actually, with a flawless local emulation > of python, except that... its far too slow. An empty loop over a range of > 30k will take about a half a second on a decent machine. =[ So, its nowhere > near as extensible as we'd like. emscripten is really interesting; i can't imagine the performance issue will improve all that much though. > So, our next hope is pyjs. The idea here is instead of mirroring the python > game code on the client side directly, we'll translate it into javascript > instead. However, I'm at a loss of not only how to evaluate this > possibility but even where to begin, lol. take a look at the helloworld example -- translate it with `pyjsbuild --enable-dynamic-link Hello.py`, then look at the output/ and lib/ directories, starting with Hello.html ... that will rapidly give you a good introduction. > As far as I can tell, the pyjs project has both a python->javascript > translator as well as some sort of widget interface compatible with GWT. correct. > The docs say that the translator is stand-alone, but all documentation > examples seem to use the widget stuff. (we have our own UI already set up > built with jQuery that we're happy with, so we're only interested in the > python -> javascript translation part of pyjs). Something called > 'pyjampiler' seems to be for stand-alone translation, but there is no > documentation (that I can find) for it. I can not get the one from the > github to work, although I was able to compile the example in this older > version of pyjampiler I found here: http://www.smallbulb.net/pyjampiler > However, this one seems critically out of date compared to the one in the > github. tbh, i don't know why pyjampiler exists or what it can do over pyjscompile, the latter being whats used by pyjsbuild, im 95% confident it's cruft and will be cut soon. note: pyjsbuild is like `gcc` (a wrapper + "packager"), and pyjscompile is like `cc` (raw translation). pyjscompile is what you are looking for, but you will have to ensure all supporting libraries and the python runtime itself gets properly translated, else stuff will not work. pyjscompile translates whole modules only: py module in, js "module" out. > So, my questions: > > 1.) Can I infact use the pyjs translator stand-alone without the widget > stuff? If so, how do I use it? Do I do it with pyjampiler or some other > way? you must use pyjscompile to translate your modules, all their dependencies, and and the stdlib manually. each modules dependencies will be appended as a comment to the output (do the helloworld example above, you'll see). aside from the translated modules, you'll need `pyjs/src/pyjs/builtin/public/_pyjs.js` (implements the most basic operations in js needed to bootstrap), and *some* of `pyjs/src/pyjs/builtin/public/bootstrap.js` (responsible for kickstarting the whole process, and actually starting the "python" runtime). > 2.) How good is the translator? Is it basically production-ready, or are > there some issues? I could help fix the issues if there are any significant > ones beyond what I found on the Migration guide wiki page. (the limitations > there being perfectly fine with me - my real worst fear is getting > complaints about games that function correctly without lag compensation > turned on but then are bugged (or can't compile) with it turned on.) the translator is pretty solid. it has seen much activity and use over several years. the python language is implemented in 95% of all ways that you'll care about. 2% is not, 2% makes it really slow, and the remaining 1% is really hard ... barely making sense in the browser anyway (exec/eval/etc). > 3.) How interfacable with normal javascript is the output of pyjs? Can I, > for instance, construct an array with a simple for loop: > > var a = new Array(); for (var i=0; i<10; i++) { a[i] = i; } > > and then pass that directly to a function that was created with pyjs? I'm > guessing not, but is there some kind of interface that will translate a > javascript object to a pyjs javascript object (even via JSON is fine)? no you cannot, and no there isn't. while, with some determination, you can discover the proper calls to make this work, it's not an interface by any means and subject to change at any time. this idea of a JS interface is one that has come up now and again (probably just a week ago as well) ... there is no immediate plans for such an interface, but i have been kicking around ideas on how it might work ... alas, i'm afraid that's far from "implemented". > Anyways, apologies if I missed the answer to any of this in the docs > somewhere, but I would really appreciate any help you got! no worries, the docs are in very poor shape right now, with both outdated and flat out incorrect information. this is improving rapidly, but if you find specific, please raise a report :-) im not sure i've understood your requirements completely (any links/etc?), but i would recommend starting with pyjsbuild and the simple examples just to get a feel for how it all works. you could even take the artifacts from pyjsbuild and manually add them to your application, trying to make it all work, before you worry about pyjscompile. we'll be around if [when] you have more questions :-) -- C Anthony
