Server-side Sessions in Flask with Redis

Flask Tip:

Flask-Session works great with a Redis database!

After configuring the interface to Redis, the session object can be used (but data is stored on the server!).


import redis
from flask import Flask, session, render_template_string
from flask_session import Session

# Create the Flask application
app = Flask(__name__)

# Configure Redis for storing the session data on the server-side
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_PERMANENT'] = False
app.config['SESSION_USE_SIGNER'] = True
app.config['SESSION_REDIS'] = redis.from_url('redis://localhost:6379')

# Create and initialize the Flask-Session object AFTER `app` has been configured
server_session = Session(app)

def get_email():
    return render_template_string("""<h1>Welcome {{ session['email'] }}!</h1>""")

For more, review Server-side Sessions in Flask with Redis.