简体   繁体   English

通过Javascript调用Python方法

[英]Call Python method through Javascript

I am trying to call a simple python method through JavaScript. 我试图通过JavaScript调用一个简单的python方法。 But I am not able to call that method. 但是我无法调用该方法。 Here is my code of Python: 这是我的Python代码:

def main():
    return   "Hello from Python"

Here is my JS: 这是我的JS:

<script>
function call() {
    $.ajax({
        type: "POST",
        url: "/gadget/call.py",
        success: callbackFunc
    });
}

function callbackFunc(response) {
    console.log(response);
}
</script>

My ajax function reads the python file as text file and return the complete text of that file. 我的ajax函数将python文件读取为文本文件,并返回该文件的完整文本。 It does not execute python method. 它不执行python方法。

If someone have any idea to call a python file through JavaScript. 如果有人有想法通过JavaScript调用python文件。 Please help me on it. 请帮我。

Thanks 谢谢

You can create a REST API (such as flask) in python, and then the javascript will call this API using ajax- and then, the flask server will execute your python code. 您可以在python中创建REST API(例如flask),然后javascript将使用ajax-调用此API,然后flask服务器将执行您的python代码。 More about flask: http://flask.pocoo.org/ . 有关烧瓶的更多信息: http : //flask.pocoo.org/ Enjoy! 请享用!

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

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