Reputation: 5965
I'm building a data analysis Flask application which takes a ton of user inputs, performs some calculations then projects the results to various web pages. I'm using a pandas Dataframe to store the inputs and perform the calculations. Then I convert the DF to a dictionary and store that in the session object.
I'm having problems due to the fact that the session object can only hold ~4k bytes. Several pages read the data so I need a means to pass this large amount of data (~5k-50k) from one request to another (which the session object does perfectly but for smaller memory sizes).
Can I set the storage limit higher for the session object (I imagine I can't as 4k is the limit for a cookie and the session object is a cookie)? Or is there something else I should do here (store the dict in a database, etc.)?
EDIT:
I'm thinking a viable alternative would be to grab the data from the database (mongodb in my case), store it in a local variable and pass that variable to the template directly. Are there negatives to this? And is there a limit on how much memory I can pass directory to a template? See example below:
@app.route('/results')
def results():
# get data I need from database (~5k-50k bytes)
data = mongo.db[collection_name].find_one({'key': 'query'})
# pass directory to template (instead of storing in session object)
return render_template('results_page.html', data=data)
Upvotes: 8
Views: 11320
Reputation: 1629
Flask-Session adds support to Server-side Session.
Here is how you can start Flask-Session (extracted from the official documentation).
from flask import Flask, session
from flask.ext.session import Session
app = Flask(__name__)
# Check Configuration section for more details
SESSION_TYPE = 'redis'
app.config.from_object(__name__)
Session(app)
@app.route('/set/')
def set():
session['key'] = 'value'
return 'ok'
@app.route('/get/')
def get():
return session.get('key', 'not set')
Regarding the size limit on rendering templates, the official Flask documentation has no mention of this. I don't really believe there is any size limit actually.
If there is any limitation, that might be on Jinja's side.
Upvotes: 1
Reputation: 2031
Yeah this definitely sounds like a case for server-side sessions.
There are code snippets on the official site for the most popular databases.
These shouldn't be hard to migrate to since they use the same SessionMixin
interface as the cookie session system.
An even easier approach could be to use Flask-KVSession, which claims that
Integration with Flask is seamless, once the extension is loaded for a Flask application, it transparently replaces Flask’s own Session management. Any application working with sessions should work the same with Flask-KVSession
Upvotes: 5