4

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!

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
    CommentedMay 16, 2011 at 13:15
  • 1
    Perhaps it helps if you tell us what it is exactly that you want to achieve.CommentedMay 16, 2011 at 13:19
  • Why the downvote? It's a legitimate question.
    – Frode
    CommentedMay 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
    CommentedMay 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
    CommentedMay 16, 2011 at 15:14

2 Answers 2

3

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
  • I think the added complexity probably exceeds the effort to rewrite the function, especially if the OP is using windows, where V8 is notoriously difficult to compile.
    – mikerobi
    CommentedMay 16, 2011 at 14:04
  • If it is ported, then you have to maintain two versions of it.
    – Quentin
    CommentedMay 16, 2011 at 14:08
  • That is esactly the reason. I don't want to maintain two versions of the same function. That will result in problems sooner or later.
    – Walter
    CommentedMay 16, 2011 at 14:48
1

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) 

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.