I am creating a web app using Flask.
I wonder if it is possible to save user session data like
session['ishappy'] = true
in database like it's done in Django using SessionMiddleware where you have options to choose between cookies and database.
And if it is what should I import to my Flask app.
You should check out Flask-KVSession, which is self-described as
Which basically describes traditional server-side sessions. Note that it supports multiple database backends:
See the Example Use for an example of what to import and how to configure it.
One of colleagues recently posted the post, which explains how to work and share the user sessions. Hope that helps:
I suggest you implement your own Session and SessionInterface by subclassing flask defaults. Basically, you need to define your own session class and a session interface class.
The above class will now have a session id (sid) that will be stored in the cookie. All the data related to this session id will be stored in your mysql database. For that, you need to implement the following class and methods below:
Also, you can define a model for storing session data:
The following snippets should give you an idea:
http://flask.pocoo.org/snippets/75/
http://flask.pocoo.org/snippets/86/
http://flask.pocoo.org/snippets/110/