I have a flask app that gets it's data from am oracle database using a SQL request. That database is updated very infrequently, so my idea was to get those data from the database and perform many manipulations of those data when the application loads. Then the web pages are very fast because I do not have make a SQL request again, and I also have no fear of SQL injection. However, I do need to update the data about once a day.
Below is a minimum verifiable example of the code which does not use SQL, but whichshould still work to demonstrate the principle.
In my real code df come from the database, but I create multiple variables from that that get passed into the web page using flask. How can I refresh these?
I tried this
How to re-run my python flask application every night?
but the server never refreshed, even when I put debug mode = False. The reason it doesn't work is that once the flask app is running it's like it's in it's own while loop serving the app. The first time I press ctrl+c it exits the app server and starts the while loop again, restarting the server, but that doesn't help.
from flask import Flask, render_template, redirect, url_for, Response
import pandas as pd
import datetime as dt
import flask
#####################################################################
#start of part that I need to refresh
df= pd.DataFrame(range(5),columns=['Column1'])
df['Column2'] = df['Column1']*2
df['Column3'] =dt.datetime.now()
df['Column4'] = df['Column3'] + pd.to_timedelta(df['Column2'],'d')
var1=min(df['Column4'])
var2=max(df['Column4'])
#End of Refresh
####################################################################
app = flask.Flask(__name__, static_url_path='',
static_folder='static',
template_folder='template')
app.config["DEBUG"] = True
@app.route("/home")
def home():
return render_template("home.html",
var1=str(var1),
var2=str(var2))
if __name__ == '__main__':
app.run(host="localhost", port=5000, debug=True)
HOME HTML
'''
<HTML>
<body>
<div>This is Var1: {{var1}} </div>
<div>This is Var2: {{var2}} </div>
</body>
</HTML>
'''
I would like to make a working example that refreshed every 5 minutes as a proof of concept.