Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass complex objects across view functions/sessions in Flask

Tags:

python

flask

I am writing a web application that receives a lot of data from a third party server, when (and only when) a user logs in. This data is parsed to custom objects and stored in a list(). Now the user works with this data all around the application, calling different views (e.g. sending different requests). I'm not sure what's the best pattern to pass the list of objects between the view functions?

I technically see two possibilities, but both have drawbacks in my case:

  1. The session dict: Storing the data in the session is an overkill (the whole list would be send back and forth between server and browser on every request)
  2. Persisting: Temporarly persisting the data to a database seem more adequate. But I was hoping to not having to use a database at all (except for this temporarly data I don't have any data that needs to be stored locally. Everything else is received from the third party server and sent back to it).

I'm not a very expirienced web developer, so maybe I oversee the obvious. So is there another way to pass the data between requests? Maybe some built in flask magic or is persisting (to a file or database) really the only option?

like image 687
cyphorious Avatar asked Sep 18 '13 13:09

cyphorious


People also ask

Can you store objects in flask session?

While it may be an over-simplification, storing data in the session object in Flask can be considered client-side storage, since the data is stored in the web browser.

How do I manage my sessions 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 do I get data from a session in flask?

To release a session variable use pop() method. The following code is a simple demonstration of session works in Flask. URL '/' simply prompts user to log in, as session variable 'username' is not set. As user browses to '/login' the login() view function, because it is called through GET method, opens up a login form.

Is flask session unique for each user?

Each session has a Session ID (encrypted with a secret key). Sessions use a unique id to retrieve the stored values. Whenever a session is created, a cookie containing the unique session id is stored on the user's computer. and is returned with every request to the server.


1 Answers

Although Flask's default implementation for sessions is to store the data in a cookie, that's not the only way to do it. Typically, you store a session ID in the cookie, and the data itself is stored somewhere on the server and retrieved via that cookie.

Flask does provide you with an easy way of overriding the default session implementation, and there are various recipes around showing how to do that - here's an SO question that shows the outline.

like image 127
Daniel Roseman Avatar answered Oct 21 '22 15:10

Daniel Roseman