Вопрос

essentially I am writing something based off of python and I would like to, in python, be able to get the result of a javascript function.

Lets say function.js has a bunch of functions inside it

If I have some python code, in it I would like to be able to do something like the following:

val = some_js_function(param1,param2,...paramn)

now some_js_function would be a function from the function.js file. This would set the variable val in my Python code to the result of that JS function.

How could I go about doing this? Or do I have to rawCode a FFI for javascript myself.

Это было полезно?

Решение

You can take a look at some java script interpreter that has support for python. You can take a look at Rhino, Google's V8 (pyV8), or even pynarcissus.

Example from pyV8,

>>> import PyV8
>>> ctxt = PyV8.JSContext()          # create a context with an implicit global object
>>> ctxt.enter()                     # enter the context (also support with statement)
>>> ctxt.eval("1+2")  

Другие советы

You could start a subprocess that calls "node function.js" or any other interpreter.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top