如何将变量从python脚本传递到flask应用程序

时间:2020-10-06 15:16:13

标签: python jquery ajax flask

我正在尝试将变量从python脚本传递到我的flask应用程序,以便可以在我的网络服务器上显示

以下是python脚本(helloworldtest.py):

import json

variable = "Hello World"

def hello():
  return variable


if __name__ == '__main__':
    hello()

我用来检索此数据的ajax调用如下:

function getData()
{
    
    // FIX THIS
    $.ajax
         (
            {
            type: "get",
            url : "/static/helloworldtest.py",
            datatype: "jsonp",
            success: function(response)
                {
                    var output = response;
                    console.log(output);
                    document.getElementById('hello').innerHTML = output;
                }
            }
        );

};

</script>
</head>
<body onload="startTime()">
    <h1>BioCloud Sensor</h1>
    <h2>The date and time on the server is : <span id=clock>  </span> </h2>
    <h2> The variable sent by the script is <span id=hello> </span> </h2>
</body>

console.log(输出)返回:

import json

variable = "Hello World"

def hello():
  return variable


if __name__ == '__main__':
    hello()

如何仅获取变量?

variable =“世界你好”

返回并显示“ hello world”?

(对不起,我是一名新程序员,如果我需要发布任何其他信息,请告诉我)

1 个答案:

答案 0 :(得分:0)

我认为您需要在Flask documentation,尤其是其Quickstart页面上阅读更多内容。您正在尝试检索整个文件,但是您想要做的是路由hello方法,以便可以用ajax检索其输出。上面链接的快速入门页面上有一个很好的“ Hello World”示例。