User Sessions

class microdot.session.SessionDict(request, session_dict)

A session dictionary.

The session dictionary is a standard Python dictionary that has been extended with convenience save() and delete() methods.

save()

Update the session cookie.

delete()

Delete the session cookie.

class microdot.session.Session(app=None, secret_key=None, cookie_options=None)

Session handling

Parameters:
  • app – The application instance.

  • secret_key – The secret key, as a string or bytes object.

  • cookie_options – A dictionary with cookie options to pass as arguments to Response.set_cookie().

get(request)

Retrieve the user session.

Parameters:

request – The client request.

The return value is a session dictionary with the data stored in the user’s session, or {} if the session data is not available or invalid.

update(request, session)

Update the user session.

Parameters:
  • request – The client request.

  • session – A dictionary with the update session data for the user.

Applications would normally not call this method directly, instead they would use the SessionDict.save() method on the session dictionary, which calls this method. For example:

@app.route('/')
@with_session
def index(request, session):
    session['foo'] = 'bar'
    session.save()
    return 'Hello, World!'

Calling this method adds a cookie with the updated session to the request currently being processed.

delete(request)

Remove the user session.

Parameters:

request – The client request.

Applications would normally not call this method directly, instead they would use the SessionDict.delete() method on the session dictionary, which calls this method. For example:

@app.route('/')
@with_session
def index(request, session):
    session.delete()
    return 'Hello, World!'

Calling this method adds a cookie removal header to the request currently being processed.

microdot.session.with_session(f)

Decorator that passes the user session to the route handler.

The session dictionary is passed to the decorated function as an argument after the request object. Example:

@app.route('/')
@with_session
def index(request, session):
    return 'Hello, World!'

Note that the decorator does not save the session. To update the session, call the session.save() method.