duktape

python wrapper for duktape, an embeddable javascript library


Keywords
javascript, duktape
License
Other
Install
pip install duktape==0.0.3

Documentation

duktape-py

python wrapper for duktape, an embeddable javascript engine

demo

>>> import duktape
>>> c=duktape.DukContext()
>>> c.eval_string("""function C(a,b){this.a=a; this.b=b};
... C.prototype.tot=function(){return this.a+this.b};
... new C(1,2);""")
>>> c.get()
{'a': 1.0, 'b': 2.0}
>>> c.call_prop('tot',())
>>> c.get()
3.0
>>> def pushget(x): c.push(x); return c.get()
... 
>>> map(pushget,[1,2.,'three',[4,5],{'6':7},[[8]]])
[1.0, 2.0, 'three', [4.0, 5.0], {'6': 7.0}, [[8.0]]]
>>> c.get_global("C")
>>> c.construct(1,2)
>>> c.get()
{'a': 1.0, 'b': 2.0}

you can make python functions callable from javascript (though it leaks memory and you can't bind a 'this'):

>>> def add(a,b): return a+b
... 
>>> c.push_func(add,2)
>>> c.call(1,2)
>>> c.get()
3.0

Tread lightly: not all errors are caught. In particular, errors in a constructor function aren't handled.

installation

pip install duktape

why?

  • pyv8 is fine if you want boost on your system
  • being able to run JS from python lets you run integration tests without standing up client-server infrastructure -- makes tests faster and more reliable

warnings

  • use this for testing only
  • note the version (0.0.2). this is not mature.
  • don't run in production unless you like SIGABRT
  • this isn't full-featured; most of duktape isn't exposed
  • this is almost as low-level as duktape so you'll have to interact with the stack