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 plex 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!
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 plex 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!
Share Improve this question asked May 16, 2011 at 13:13 WalterWalter 491 silver badge2 bronze badges 8- 3 This question does not make much sense since Python is likely running somewhere on your server and Javascript is executed within the browser. Or? – user2665694 Commented May 16, 2011 at 13:15
- 1 Perhaps it helps if you tell us what it is exactly that you want to achieve. – Marcel Korpel Commented May 16, 2011 at 13:19
- Why the downvote? It's a legitimate question. – Frode Commented 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 Commented 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 Commented May 16, 2011 at 15:14
2 Answers
Reset to default 3pyv8 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.
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)