[英]Call Javascript from python
I am looking for a way to call a js function from a python function. 我正在寻找一种从python函数调用js函数的方法。 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?
我来找你,因为我也需要我的js函数来使用DOM,所以pyv8例如不是解决方案..你们有什么想法吗? Is it even possible?
它甚至可能吗?
Thanks in advance. 提前致谢。
PyExecJS seems to be a good option. PyExecJS似乎是个不错的选择。
>>> import execjs
>>> execjs.eval("'red yellow blue'.split(' ')")
['red', 'yellow', 'blue']
>>> ctx = execjs.compile("""
... function add(x, y) {
... return x + y;
... }
... """)
>>> ctx.call("add", 1, 2)
3
to install: 安装:
$ pip install PyExecJS
According to https://stackoverflow.com/a/30537286/6353933 , 根据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). Js2Py的优点包括可移植性和非常容易与python集成(因为基本上JavaScript正在被转换为python)。
To install: 安装:
pip install js2py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.