Flask: session max size too small

Viewed 8232

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)
2 Answers

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.

Related