O'Reilly logo

Instant Flask Web Development by Ron DuPlain

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling sessions and users (Intermediate)

Since HTTP is stateless, we need to track some data across requests with a session.

Getting ready

We will continue to work from the app.py file from the sched directory and the models.py file.

How to do it...

  1. Flask provides a session object, which behaves like a Python dictionary, and persists automatically across requests. You can, in your Flask application code:
    from flask import session
    # ... in a request ...
    session['spam'] = 'eggs'
    # ... in another request ...
    spam = session.get('spam') # 'eggs'
  2. Flask-Login provides a simple means to track a user in Flask's session. Update requirements.txt:
    Flask
    Flask-Login
    Flask-Script
    Flask-SQLAlchemy
    WTForms
  3. Then:
    $ pip install -r requirements.txt
    
  4. We can then load Flask-Login ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required