Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask: session max size too small

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)
like image 432
Johnny Metz Avatar asked May 16 '17 23:05

Johnny Metz


People also ask

How do you handle a session in Flask?

Flask-Session is an extension for Flask that supports Server-side Session to your application. The Session is the time between the client logs in to the server and logs out of the server. The data that is required to be saved in the Session is stored in a temporary directory on the server.

How long does Flask session last?

Default session lifetime is 31 days, user need to specify the login refresh view in case of timeout. Above line will force user to re-login every 5 minutes.

What is the difference between G variable and session in the Flask?

session gives you a place to store data per specific browser. As a user of your Flask app, using a specific browser, returns for more requests, the session data is carried over across those requests. g on the other hand is data shared between different parts of your code base within one request cycle.

How do I reset a session Flask?

There is no way to clear session or anything. One must simply change the app. config["SECRET_KEY"] and the contents in session dictionary will get erased.


1 Answers

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

like image 72
Dmiters Avatar answered Oct 10 '22 17:10

Dmiters