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:
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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With