简体   繁体   English

在python中运行js函数调用另一个js函数

[英]run js function in python which calls another js function

I know I can call JavaScript function in python with js2py like below :我知道我可以使用js2py在 python 中调用 JavaScript 函数,如下所示:

import execjs
import  js2py
un ='''
function sample(x)
{
    return x
}
'''
print(js2py.eval_js(un)("Hi"))

but my function in JavaScript calls another function, how can handle it in Python?但是我在 JavaScript 中的函数调用了另一个函数,如何在 Python 中处理它?

import execjs
import  js2py



un ='''
function sample(x)
{
    return func2(x)
}
'''
print(js2py.eval_js(un)("Hi"))

thre maybe more than 2 Functions可能超过 2 个功能

One can define multiple functions in the string.可以在字符串中定义多个函数。 Note that js2py runs the last function first .请注意,js2py 首先运行最后一个函数

import  js2py
un ='''
function func2(q) {
    return q + " World";
}
function sample(x) {
    return func2(x) + "!";
}
'''
print(js2py.eval_js(un)("Hello"))

This prints the always heartwarming Hello World!这打印出总是令人心旷神怡的Hello World!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM