你最有可能做这样的事情:
from flask import Flask, render_template
from yourMySqlLibrary import connect_to_mysql
conn = connect_to_mysql()
# This is only executed when you start the script
data = conn.execute("SELECT * FROM MySemiRegularlyUpdatedTable")
app = Flask(__name__)
@app.route("/")
def view_data():
return render_template("view_data.html", data=data)
if __name__ == "__main__":
app.run()
如果是这种情况,那么您的解决方案就是将您的连接和查询调用移动到您的控制器中,以便每次点击页面时都重新查询数据库:
@app.route("/")
def view_data():
# Removed from above and placed here
# The connection is made to the database for each request
conn = connect_to_mysql()
# This is only executed on every request
data = conn.execute("SELECT * FROM MySemiRegularlyUpdatedTable")
return render_template("view_data.html", data=data)
这样,您的视图将在您的数据更新时更新 - 您无需重新启动服务器即可获取对数据的更改。