| 1 |
Usage |
| 2 |
=========== |
| 3 |
|
| 4 |
Quickstart |
| 5 |
----------- |
| 6 |
|
| 7 |
.. currentmodule:: flask_session |
| 8 |
|
| 9 |
|
| 10 |
Create your :class:`~flask.Flask` application, load the configuration of choice, and |
| 11 |
then create the :class:`Session` object by passing it the application. |
| 12 |
|
| 13 |
.. code-block:: python |
| 14 |
|
| 15 |
from flask import Flask, session |
| 16 |
from flask_session import Session |
| 17 |
|
| 18 |
app = Flask(__name__) |
| 19 |
|
| 20 |
SESSION_TYPE = 'redis' |
| 21 |
SESSION_REDIS = Redis(host='localhost', port=6379) |
| 22 |
app.config.from_object(__name__) |
| 23 |
Session(app) |
| 24 |
|
| 25 |
@app.route('/set/') |
| 26 |
def set(): |
| 27 |
session['key'] = 'value' |
| 28 |
return 'ok' |
| 29 |
|
| 30 |
@app.route('/get/') |
| 31 |
def get(): |
| 32 |
return session.get('key', 'not set') |
| 33 |
|
| 34 |
See the configuration section for more details. |
| 35 |
|
| 36 |
.. note:: |
| 37 |
|
| 38 |
You can not use :class:`~Session` instance directly, what :class:`~Session` does |
| 39 |
is just change the :attr:`~flask.Flask.session_interface` attribute on |
| 40 |
your Flask applications. You should always use :class:`flask.session` when accessing or modifying the current session. |
| 41 |
|
| 42 |
|
| 43 |
Alternative initialization |
| 44 |
--------------------------- |
| 45 |
|
| 46 |
Rather than calling :class:`~Session`, you may initialize later using :meth:`~Session.init_app`. |
| 47 |
|
| 48 |
.. code-block:: python |
| 49 |
|
| 50 |
... |
| 51 |
sess = Session() |
| 52 |
sess.init_app(app) |
| 53 |
|
| 54 |
Or, if you prefer to directly set parameters rather than using the configuration constants, you can initialize by setting an instance of :class:`flask_session.redis.RedisSessionInterface` directly to the :attr:`flask.Flask.session_interface`. |
| 55 |
|
| 56 |
.. code-block:: python |
| 57 |
|
| 58 |
from flask import Flask, session |
| 59 |
from flask_session.redis import RedisSessionInterface |
| 60 |
from redis import Redis |
| 61 |
|
| 62 |
app = Flask(__name__) |
| 63 |
|
| 64 |
redis = Redis(host='localhost', port=6379) |
| 65 |
app.session_interface = RedisSessionInterface(client=redis) |
| 66 |
|
| 67 |
|
| 68 |
Using CacheLib as a session backend |
| 69 |
------------------------------------ |
| 70 |
|
| 71 |
.. note:: |
| 72 |
|
| 73 |
FileSystemSession was recently deprecated in favor of CacheLib, which is what is was using under the hood. |
| 74 |
|
| 75 |
The following example demonstrates how to use CacheLib as a session backend with the file system cache. This might be useful for rapid development or testing. |
| 76 |
|
| 77 |
.. code-block:: python |
| 78 |
|
| 79 |
from flask import Flask, session |
| 80 |
from flask_session import Session |
| 81 |
from cachelib.file import FileSystemCache |
| 82 |
|
| 83 |
app = Flask(__name__) |
| 84 |
|
| 85 |
SESSION_TYPE = 'cachelib' |
| 86 |
SESSION_SERIALIZATION_FORMAT = 'json' |
| 87 |
SESSION_CACHELIB = FileSystemCache(threshold=500, cache_dir="/sessions"), |
| 88 |
app.config.from_object(__name__) |
| 89 |
Session(app) |