API

class flask_session.Session(app=None)

This class is used to add Server-side Session to one or more Flask applications.

There are two usage modes. One is initialize the instance with a very specific Flask application:

app = Flask(__name__)
Session(app)

The second possibility is to create the object once and configure the application later:

sess = Session()

def create_app():
    app = Flask(__name__)
    sess.init_app(app)
    return app

By default Flask-Session will use NullSessionInterface, you really should configurate your app to use a different SessionInterface.

Note

You can not use Session instance directly, what Session does is just change the session_interface attribute on your Flask applications.

init_app(app)

This is used to set up session for your app object.

Parameters:

app – the Flask app object with proper configuration.

class flask_session.sessions.ServerSideSession(initial=None, sid=None, permanent=None)

Baseclass for server-side based sessions.

sid

Session id, internally we use secrets.token_urlsafe() to generate one session id. You can access it with session.sid.

class flask_session.NullSessionInterface

Used to open a flask.sessions.NullSession instance.

If you do not configure a different SESSION_TYPE, this will be used to generate nicer error messages. Will allow read-only access to the empty session but fail on setting.

class flask_session.RedisSessionInterface(redis, key_prefix, use_signer, permanent, sid_length)

Uses the Redis key-value store as a session backend. (redis-py required)

Parameters:
  • redis – A redis.Redis instance.

  • key_prefix – A prefix that is added to all Redis store keys.

  • use_signer – Whether to sign the session id cookie or not.

  • permanent – Whether to use permanent session or not.

  • sid_length – The length of the generated session id in bytes.

New in version 0.6: The sid_length parameter was added.

New in version 0.2: The use_signer parameter was added.

class flask_session.MemcachedSessionInterface(client, key_prefix, use_signer, permanent, sid_length)

A Session interface that uses memcached as backend. (pylibmc or python-memcached or pymemcache required)

Parameters:
  • client – A memcache.Client instance.

  • key_prefix – A prefix that is added to all Memcached store keys.

  • use_signer – Whether to sign the session id cookie or not.

  • permanent – Whether to use permanent session or not.

  • sid_length – The length of the generated session id in bytes.

New in version 0.6: The sid_length parameter was added.

New in version 0.2: The use_signer parameter was added.

class flask_session.FileSystemSessionInterface(cache_dir, threshold, mode, key_prefix, use_signer, permanent, sid_length)

Uses the cachelib.file.FileSystemCache as a session backend.

Parameters:
  • cache_dir – the directory where session files are stored.

  • threshold – the maximum number of items the session stores before it starts deleting some.

  • mode – the file mode wanted for the session files, default 0600

  • key_prefix – A prefix that is added to FileSystemCache store keys.

  • use_signer – Whether to sign the session id cookie or not.

  • permanent – Whether to use permanent session or not.

  • sid_length – The length of the generated session id in bytes.

New in version 0.6: The sid_length parameter was added.

New in version 0.2: The use_signer parameter was added.

class flask_session.MongoDBSessionInterface(client, db, collection, key_prefix, use_signer, permanent, sid_length)

A Session interface that uses mongodb as backend. (pymongo required)

Parameters:
  • client – A pymongo.MongoClient instance.

  • db – The database you want to use.

  • collection – The collection you want to use.

  • key_prefix – A prefix that is added to all MongoDB store keys.

  • use_signer – Whether to sign the session id cookie or not.

  • permanent – Whether to use permanent session or not.

  • sid_length – The length of the generated session id in bytes.

New in version 0.6: The sid_length parameter was added.

New in version 0.2: The use_signer parameter was added.

class flask_session.SqlAlchemySessionInterface(app, db, table, sequence, schema, bind_key, key_prefix, use_signer, permanent, sid_length)

Uses the Flask-SQLAlchemy from a flask app as a session backend.

Parameters:
  • app – A Flask app instance.

  • db – A Flask-SQLAlchemy instance.

  • table – The table name you want to use.

  • key_prefix – A prefix that is added to all store keys.

  • use_signer – Whether to sign the session id cookie or not.

  • permanent – Whether to use permanent session or not.

  • sid_length – The length of the generated session id in bytes.

  • sequence – The sequence to use for the primary key if needed.

  • schema – The db schema to use

  • bind_key – The db bind key to use

New in version 0.6: The sid_length, sequence, schema and bind_key parameters were added.

New in version 0.2: The use_signer parameter was added.