align-toparrow-leftarrow-rightbackbellblockcalendarcamerachatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-crosscrosseditfacebookglobegoogleimagesinstagramlocation-pinmagnifying-glassmailmoremuplabelShape 3 + Rectangle 1outlookpersonplusImported LayersImported LayersImported Layersshieldstartwitteryahoo

Re: [nycpython] PYXC-PJ: a Python to Javascript compiler

From: Andrew S.
Sent on: Saturday, October 30, 2010 10:52 AM

On Oct 29, 2010, at 8:20 PM, Patrick wrote:
How would you use a library like jquery in javascript with this? If
there's any reason too?

script src="...jquery.js"
script src="...my-compiled-code.js"

Since Python doesn't support $ as a name, you can use jQuery's other name: "jQuery"
J = jQuery
J(document).ready(...)


On Oct 30, 2010, at 7:22 AM, david jensen wrote:
Did you try making this work with Google's Closure?

Closure Compiler? Absolutely. I always minify my JS using Closure's simple-optimizations mode.

Closure Library? Sure. Just build the subset you need and include it first as another script. The pj-closure conversion is mostly for fun.

Note: sometime this weekend, PYXC-PJ will support the inclusion of raw JS files via import statements. There could also be some optionally-enabled special-casing to follow Closure's dependency graph.

How is is related to Pyjamas?

(1) Pyjamas does way too much**, messing with your output in the name of Python semantics, whereas PYXC-PJ is a direct syntax conversion and the resulting JavaScript is exactly what you expect (after quickly reading about the extremely few special cases).

** "pyjslib takes care of Python types that require a custom Javascript implementation for pyjs to work. For example, even though Python lists are similar to Javascript arrays, Python lists are converted to custom objects that implement methods such as append."

(2) I found the Pyjamas transformations source code hard to casually read, whereas with PYXC-PJ, I tried to make each transformation as independent as possible (so you can easily grok or modify the transformations)

Note: the general-purpose (Python --> Anything) code is in pj/pyxc/*, and it might be useful if you're whipping up a (Python --> Something) DSL.

Also, I used docco for literate programming.

If you're interested:






On Oct 29, 2010, at 7:51 PM, Ned Batchelder wrote:

It doesn't compile Python semantics into Javascript.  The label "Python-to-Javascript compiler" is a bit of a misnomer.  As near as I can tell, it translates "Javascript-with-a-Python-inspired-syntax" into Javascript.

True. I'll try to improve the wording.


On Oct 29, 2010, at 7:32 PM, Matt Di Pasquale wrote:

Cool. Is there something like this to convert Ruby into JavaScript?


Its author wrote yesterday: "I abandoned it when CoffeeScript appeared, but you might find something interesting there."


On Oct 29, 2010, someone wrote off-list:

Just curious -- any particular reason you wrote this instead of just
using Coffeescript?

  • Python seems more readable to me
  • Python static analysis code will apply to my JavaScript too
  • No mental switching cost when cycling between client-side and server-side window groups
  • Compilers are fun to write, especially when you try to make the transformations readable and independent


- Andrew

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy