Skip to content

Tim232/PyExecJS

 
 

Repository files navigation

PyExecJS (EOL)

Build Status

Run JavaScript code from Python.

PyExecJS is a porting of ExecJS from Ruby. PyExecJS automatically picks the best runtime available to evaluate your JavaScript program.

A short example:

import execjs
execjs.eval("'red yellow blue'.split(' ')")
>>> ['red', 'yellow', 'blue']
ctx = execjs.compile("""
     function add(x, y) {
         return x + y;
     }
 """)
 ctx.call("add", 1, 2)
 >>> 3

Supported runtimes

First-class support (runtime class is provided and tested)

Second-class support (runtime class is privided but not tested)

Installation

$ pip install PyExecJS

Details

If EXECJS_RUNTIME environment variable is specified, PyExecJS pick the JavaScript runtime as a default:

>>> execjs.get().name # this value is depends on your environment.
>>> os.environ["EXECJS_RUNTIME"] = "Node"
>>> execjs.get().name
'Node.js (V8)'

You can choose JavaScript runtime by execjs.get():

>>> default = execjs.get() # the automatically picked runtime
>>> default.eval("1 + 2")
3
>>> import execjs.runtime_names
>>> jscript = execjs.get(execjs.runtime_names.JScript)
>>> jscript.eval("1 + 2")
3
>>> import execjs.runtime_names
>>> node = execjs.get(execjs.runtime_names.Node)
>>> node.eval("1 + 2")
3

The pros of PyExecJS is that you do not need take care of JavaScript environment. Especially, it works in Windows environment without installing extra libraries.

One of cons of PyExecJS is performance. PyExecJS communicate JavaScript runtime by text and it is slow. The other cons is that it does not fully support runtime specific features.

PyV8 might be better choice for some use case.

Changelog

1.0.0

  • First release.

About

Run JavaScript code from Python (EOL: https://gist.github.com/doloopwhile/8c6ec7dd4703e8a44e559411cb2ea221)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 99.3%
  • Shell 0.7%