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 uuid.uuid4() to generate one session id. You can access it with session.sid.

class flask_session.NullSessionInterface

Used to open a flask.sessions.NullSession instance.

class flask_session.RedisSessionInterface(redis, key_prefix, use_signer=False, permanent=True)

Uses the Redis key-value store as a session backend.

New in version 0.2: The use_signer parameter was added.

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.

class flask_session.MemcachedSessionInterface(client, key_prefix, use_signer=False, permanent=True)

A Session interface that uses memcached as backend.

New in version 0.2: The use_signer parameter was added.

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.

class flask_session.FileSystemSessionInterface(cache_dir, threshold, mode, key_prefix, use_signer=False, permanent=True)

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

New in version 0.2: The use_signer parameter was added.

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.

class flask_session.MongoDBSessionInterface(client, db, collection, key_prefix, use_signer=False, permanent=True)

A Session interface that uses mongodb as backend.

New in version 0.2: The use_signer parameter was added.

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.

class flask_session.SqlAlchemySessionInterface(app, db, table, key_prefix, use_signer=False, permanent=True)

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

New in version 0.2.

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.