Reputation: 39
I'm working on a flask web application and I'm new in flask. My problem is that I want to do some things only once. For example to instantiate SearchClass and SC.refreshArray() which should be independent on refreshing my web page to increase a speed of application. Will you give me an advice how to manage that? This is my code:
from flask import Flask
from flask import request
from flask import render_template
from SearchClass import *
from Database import getConnection
import pickle
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('my-form.html')
def fileSuffixArray():
with open("saf.txt", 'rb') as f:
my_list = pickle.load(f)
print(my_list)
@app.route('/', methods=['POST'])
def search():
text = request.form['text']
db = getConnection("db")
SC = SearchClass(db)
SC.refreshArray()
results=SC.getPhrase(text)
s=""
for i in range(0,len(results)):
resString=""
res=Database.searchForExactTranslation(results[i], db)
resString= ' '.join(res)
s+="<b>%s: </b> %s<br>" % (results[i], resString)
return s
app.debug = True
if __name__ == '__main__':
app.run()
Thanks in advance!
Upvotes: 2
Views: 246
Reputation: 261
Move your code to set up the database connection to a function and call the function before calling app.run()
SC = None
def setupdb():
global SC
db = getConnection("db")
SC = SearchClass(db)
SC.refreshArray()
if __name__== "__main__":
setupdb()
app.run()
Upvotes: 1