views:

1144

answers:

7

Is it possible to integrate Python and JavaScript? For example, imagine you wanted to be able to define classes in JavaScript and use them from Python (or vice versa). If so, what's the best way? I'm interested not only if this is possible but if anyone has done it within a "serious" project or product.

I'm guessing it would be possible using Jython and Rhino, for one example, but I'm curious whether or not anyone's ever actually done this, and if there are solutions for other platforms (especially CPython).

+3  A: 

Here's something, a Python wrapper around the SeaMonkey Javascript interpreter... http://pypi.python.org/pypi/python-spidermonkey

David Zaslavsky
This seems like the closest thing to what I was looking for; thanks.
Jacob Gabrielson
+1  A: 

There's a bridge based on JavaScriptCore (from WebKit), but it's pretty incomplete: http://code.google.com/p/pyjscore/

Miles
+7  A: 

How about Pyjamas?

From the above website:

Pyjamas provides a python-to-javascript Compiler and a Web Widget set, the combination of which allows developers to easily write well-designed desktop-like Rich Media Applications in python classes and modules that will execute in all major web browsers. without having to write a single line of javascript. Pyjamas is a port of Google Web Toolkit.

+4  A: 

If your just interested in sharing complex data types between javascript and python, check out jsonpickle. It wraps the standard Python JSON libraries, but has some smarts in serializing and deserializing Python classes and other data types.

Quite a few Google App Engine projects have used this library. Joose and FirePython both incorporate jsonpickle.

John Paulett
Note that jsonpickle uses a non-standard "py/ref" reference type that makes it harder to use for non-python clients. There's currently no way to turn it off.http://code.google.com/p/jsonpickle/issues/detail?id=9
pimlottc
A: 

You might also want to check out the PyPy project - they have a Python to (anything) compiler, including Python to Javascript, C, and llvm. This allows you to write your code in Python and then compile it into Javascript as you desire.

http://codespeak.net/pypy

Also, check out the informative blog:

http://morepypy.blogspot.com/

Unfortunately though, you can't convert Javascript to Python this way. It seems to work really well overall, they used to have a Javascript (made from compiled Python) version of the Bub'n'Bros game online (though the server has been down for a while).

http://bub-n-bros.sourceforge.net

+2  A: 

there are two projects that allow an "obvious" transition between python objects and javascript objects, with "obvious" translations from int or float to Number and str or unicode to String: PyV8 and, as one writer has already mentioned: python-spidermonkey.

there are actually two implementations of pyv8 - the original experiment was by sebastien louisel, and the second one (in active development) is by flier liu.

my interest in these projects has been to link them to pyjamas, a python-to-javascript compiler, to create a JIT python accelerator.

so there is plenty out there - it just depends what you want to do.

A: 

You might also want to check out the PyPy project

unfortunately, the pypy project have ceased development of the -to-javascript backend, and have abandoned it. treating javascript as assembly language, and squeezing it through restricted-python first, made a bit of a dog's dinner, unfortunately.