I am trying to use sessions to share variables among routes in Flask. This is the code I am using:
from flask import Flask, jsonify, request, session
from flask_cors import CORS
from flask_session import Session
app = Flask(__name__)
CORS(app)
SESSION_TYPE = 'filesystem'
app.config.from_object(__name__)
app.secret_key = 'abc'
@app.route("/getinputData", methods = ['POST'])
def getData():
inputData = json.loads(request.data)
molID = inputData["ID"]
searchType = inputData["searchType"]
session["sharedmolID"] = molID
session["sharedsearchType"] = searchType
return inputData
@app.route("/querydatabyMol", methods = ['GET'])
def querydatabyMol():
sharedmolID = session.get("sharedmolID", None)
sharedsearchType = session.get("sharedsearchType", None)
print(sharedmolID)
print(sharedsearchType)
return sharedmolID
if __name__ == '__main__':
app.run(debug=True)
sess = Session()
sess.init_app(app)
But when the second route prints the variables I get None. I have temporarily solved this using global variables, but I read that they are not thread safe, so I would like to use sessions.
Any idea of what is wrong here?