-1

I have file javascript and python, I want to call javascript function in python code with send variable from python. example my javascript file

var msg = 'this is the encrypt key, very long character';
function split (value, msg){
    if(value == msg){
        return true;
    }else{
        return false;
    }
}

function process(value){
    var a = split(value, msg);
    if (a == "true"){
         return a;
    }else{
         return "Error";
    }
}

now I want to add this file to my python code, I want to send variable from python to my javascript code. example like this

value = "aaa"
js = open('example.js','r')
print(js.process(value))

I want to import javascript file to python code, and I want to use the process function in the javascript file. I know my code is wrong, I don't understand how to make a function like that. Please help me

Zmx Atah
  • 85
  • 1
  • 7

1 Answers1

0

You need a wonderful module named as js2py. Install by :

pip install js2py

The code looks like this :

import js2py
process=js2py.eval_js('''function process(value,a){
    if (a == "true"){
         return a;
    }else{
         return "Error";
    }
}''')

split=js2py.eval_js('''function split (value){
    var msg = 'this is the encrypt key, very long character';
    if(value == msg){
        return true;
    }else{
        return false;
    }
}''')
value="aaa"
a=split(value)
print(process(value,a))
Yash Makan
  • 706
  • 1
  • 5
  • 17