【问题标题】:Invoke a external python script from a html code从 html 代码调用外部 python 脚本
【发布时间】:2022-11-10 16:37:29
【问题描述】:

我在 HTML 中创建了一个按钮,我想在单击该按钮时调用外部 python 脚本。

如果可能的话,还可以将一些变量传递到该 python 脚本中。

哪种方法最简单,最快。 TIA

我尝试了烧瓶,但这对我没有帮助,我的 html 代码托管在 apache 上。我不能/不应该(也许)使用烧瓶

【问题讨论】:

    标签: python html


    【解决方案1】:

    您可以使用 ajax 来调用 python 脚本。

    <script>
    function callPythonScript() {
        $.ajax({
            url: "script.py",
            type: "POST",
            data: {
                "var1": "value1",
                "var2": "value2"
            },
            success: function(response) {
                console.log(response);
            },
            error: function(xhr) {
                console.log(xhr);
            }
        });
    }
    </script>
    

    如何在 python 脚本中接收值:

    import cgi
    form = cgi.FieldStorage()
    var1 = form.getvalue('var1')
    var2 = form.getvalue('var2')
    

    【讨论】:

      猜你喜欢
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 2012-08-16
      • 2016-11-26
      相关资源
      最近更新 更多