I have a javascript function that I can call with input and that returns a result. I can integrate this javascript function into an html-document (of course).
Now I would like to call exactly this javascript function from python. I have a python programm and want to call the javascript function with input parameters passed to the JS function by the calling python function. And the JS function shall return some result to python.
This JS function has quite complex functionality and is used in a web project also. I would like to use same functionality in Python.
Does anyone know how to solve this? Python is quite huge so I think I only did not find the required python module up until know. I spent 2 days in searching a possibility.
Thanks!
-
3This question does not make much sense since Python is likely running somewhere on your server and Javascript is executed within the browser. Or?user2665694– user26656942011年05月16日 13:15:19 +00:00Commented May 16, 2011 at 13:15
-
1Perhaps it helps if you tell us what it is exactly that you want to achieve.Marcel Korpel– Marcel Korpel2011年05月16日 13:19:20 +00:00Commented May 16, 2011 at 13:19
-
Why the downvote? It's a legitimate question.Frode– Frode2011年05月16日 13:33:19 +00:00Commented May 16, 2011 at 13:33
-
@Frode, probably because it's not a very useful question. Asking "How do I eat an apple when I only have a banana?" is also a valid question.mikerobi– mikerobi2011年05月16日 13:59:57 +00:00Commented May 16, 2011 at 13:59
-
@mikerobi, Walter has a library written in Javascript that he wants to utilize in his Python script. I just don't see how asking for ways to do this not useful and relevant on stackoverflow, but fair enough. +1 from me.Frode– Frode2011年05月16日 15:14:23 +00:00Commented May 16, 2011 at 15:14
2 Answers 2
pyv8 will let you use the V8 JS engine from Python.
If the JavaScript function depends on things that are not core JavaScript (such as DOM) then you will need to find implementations of those.
3 Comments
I had similar requirement recently and solved via node.
parser.js
module.exports = {decode}; // entry for node
function decode(payload) {
var decoded = {};
// ...
console.log(JSON.stringify(decoded));
return decoded;
}
terminal
node -e 'require("./parser.js").decode("foobar")'
output: the parsed data
caller.py
import subprocess
cmd = """node -e 'require(\"./parser.js\").decode(\"{}\")'"""
output = subprocess.check_output(cmd.format(data), shell=True)