I am dealing with a huge dataframe. I would like to avoid pickling in-between user queries. Want to know if i can save the DataFrame in Flask Session and access it from session hence avoiding pickling.
I wrote the below code but i am faced with the error:
[17578 rows x 319 columns] is not JSON serializable
#=====================================================================================
#=====================================================================================
@app.route('/start', methods=['GET', 'POST'])
def index():
if 'catalogueDF' in session:
if request.method == 'POST':
query = request.get_json('query') # Read user query
df = session['catalogueDF']
result = str(list(set(df['brandname']))[2])
else:
query = request.args.get('query')
result = 'User query: '+str(query)
else:
df = pd.read_excel('errorfree.xlsx', sheetname='Sheet1').fillna('NA')
df = pd.DataFrame([df[col].astype(str, na=False).str.lower() for col in df]).transpose()
session['catalogueDF'] = df
result = 'no query posted yet'
response = app.response_class(
response=json.dumps(result),
status=200,
mimetype='application/json'
)
return response
# Flask start of app
if __name__ == '__main__':
app.secret_key = os.urandom(24) # Sessions need encryption
app.run(debug = True)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…