Call javascript external function from python

I have a javascript function that I can call with input and returns the result. I can integrate this javascript function into an html document (of course).

Now I would like to name exactly this javascript function from python. I have a python program and you want to call a javascript function with input parameters passed to the JS function by the calling python function. And the JS function will return some result in python.

This JS function has rather complicated functionality and is also used in a web project. I would like to use the same functionality in Python.

Does anyone know how to solve this? Python is quite large, so I think that only I did not find the required python module until I recognized it. I spent 2 days looking for opportunities.

Thank!

+3
source share
2 answers

pyv8 will let you use the V8 JS engine from Python.

If the JavaScript function depends on what is not basic JavaScript (e.g. DOM), you will need to find their implementation.

+4
source

I had a similar requirement recently and was solved using 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")'

conclusion: analyzed data

caller.py

import subprocess

cmd = """node -e 'require(\"./parser.js\").decode(\"{}\")'"""
output = subprocess.check_output(cmd.format(data), shell=True)
0
source

All Articles