[英]Python Flask: how to make an endpoint refresh every second
I've created a Flask API which I want to be hit/refreshed at every second.我创建了一个 Flask API 我希望每秒都被点击/刷新。
Following is the code:以下是代码:
from flask import Flask
from datetime import datetime
app = Flask(__name__)
@app.route("/", methods=["GET"])
def home():
return """Hello World!<br>The current time is {}.""".format(datetime.strftime(datetime.now(), "%d %B %Y %X"))
if __name__ == "__main__":
app.run()
What is desired?想要什么?
I basically want the html page to update every second, showing the current date & time, ie., updating every second.我基本上希望 html 页面每秒更新一次,显示当前日期和时间,即每秒更新一次。 I haven't added any explicit HTML page for rendering.
我没有添加任何明确的 HTML 页面进行渲染。 I don't know HTML and therefore wanted to get this done via python and flask apis.
我不知道 HTML,因此想通过 python 和 flask api 来完成这项工作。
How can I achieve this?我怎样才能做到这一点? Is there any better way of doing this?
有没有更好的方法来做到这一点?
You can try this one:你可以试试这个:
from flask import Flask
from datetime import datetime
app = Flask(__name__)
@app.route("/", methods=["GET"])
def home():
return """
<meta http-equiv="refresh" content="1" />
Hello World!<br>The current time is {}.""".format(datetime.strftime(datetime.now(), "%d %B %Y %X"))
if __name__ == "__main__":
app.run()
Change the content
attribute inside the meta
tag if you want to change the frequency (the number refers to number of seconds)如果要更改频率,请更改
meta
标记内的content
属性(数字指秒数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.