Python 是否可以在服务器端的 Flask 中动态更新呈现的模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26536187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:36:45  来源:igfitidea点击:

Is it possible to dynamically update a rendered template in Flask, server-side?

pythonjsonflask

提问by damienstanton

I currently have a Flask web server that pulls data from a JSON API using the built-in requests object.

我目前有一个 Flask Web 服务器,它使用内置的请求对象从 JSON API 中提取数据。

For example:

例如:

def get_data():
    response = requests.get("http://myhost/jsonapi")
    ...
    return response

@main.route("/", methods=["GET"])
def index():
    return render_template("index.html", response=response)

The issue here is that naturally the GET method is only run once, the first time get_data is called. In order to refresh the data, I have to stop and restart the Flask wsgi server. I've tried wrapping various parts of the code in a while True / sleep loop but this prevents werkzeug from loading the page.

这里的问题是 GET 方法自然只运行一次,即第一次调用 get_data。为了刷新数据,我必须停止并重新启动 Flask wsgi 服务器。我尝试将代码的各个部分包装在 while True / sleep 循环中,但这会阻止 werkzeug 加载页面。

What is the most Pythonic way to dynamically GET the data I want without having to reload the page or restart the server?

无需重新加载页面或重新启动服务器即可动态获取我想要的数据的最 Pythonic 方法是什么?

采纳答案by Jonathan Eunice

You're discussing what are perhaps two different issues.

您正在讨论可能是两个不同的问题。

  1. Let's assume the problem is you're calling the dynamic data source, get_data(), only once and keeping its (static) value in a global response. This one-time-call is not shown, but let's say it's somewhere in your code. Then, if you are willing to refresh the page (/) to get updates, you could then:

    @main.route("/", methods=['GET'])
    def index():
        return render_template("index.html", response=get_data())
    

    This would fetch fresh data on every page load.

  2. Then toward the end of your question, you ask how to "GET the data I want without having to reload the page or restart the server." That is an entirely different issue. You will have to use AJAX or WebSocket requests in your code. There are quite a few tutorials about how to do this (e.g. this one) that you can find through Googling "Flask AJAX." But this will require an JavaScript AJAX call. I recommend finding examples of how this is done through searching "Flask AJAX jQuery" as jQuery will abstract and simplify what you need to do on the client side. Or, if you wish to use WebSockets for lower-latency connection between your web page, that is also possible; search for examples (e.g. like this one).

  1. 让我们假设问题是您只调用动态数据源get_data()一次并将其(静态)值保留在全局response. 这个一次性调用没有显示,但假设它在您的代码中的某个地方。然后,如果您愿意刷新页面 ( /) 以获取更新,则可以:

    @main.route("/", methods=['GET'])
    def index():
        return render_template("index.html", response=get_data())
    

    这将在每次页面加载时获取新数据。

  2. 然后在您的问题结束时,您询问如何“无需重新加载页面或重新启动服务器即可获取我想要的数据”。那是一个完全不同的问题。您必须在代码中使用 AJAX 或 WebSocket 请求。您可以通过谷歌搜索“Flask AJAX”找到很多关于如何执行此操作的教程(例如本教程)。但这将需要 JavaScript AJAX 调用。我建议通过搜索“Flask AJAX jQuery”来查找如何完成此操作的示例,因为 jQuery 将抽象并简化您需要在客户端执行的操作。或者,如果您希望使用 WebSockets 在您的网页之间进行低延迟连接,那也是可能的;搜索示例(例如像这样的)。