admin管理员组文章数量:1023815
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)
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)
本文标签: call an external javascript function from pythonStack Overflow
版权声明:本文标题:call an external javascript function from python - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745601897a2158505.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论