Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Javascript from python

I am looking for a way to call a js function from a python function. I am coming to you because I also need my js function to use DOM, so pyv8 for example is not a solution.. Do you guys have any idea? Is it even possible?

Thanks in advance.

like image 231
souki Avatar asked Dec 24 '22 02:12

souki


1 Answers

According to https://stackoverflow.com/a/30537286/6353933,

import js2py

js = """
function escramble_758(){
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write(a+c+b)
}
escramble_758()
""".replace("document.write", "return ")

result = js2py.eval_js(js)  # executing JavaScript and converting the result to python string 

Advantages of Js2Py include portability and extremely easy integration with python (since basically JavaScript is being translated to python).

To install:

pip install js2py
like image 50
Kode.Error404 Avatar answered Jan 06 '23 18:01

Kode.Error404