Reputation: 2776
I'm deploying a Flask application on Heroku and need to implement server-side sessions for various reasons, and I can't figure out how to properly set it up. Heroku's docs on how to connect are quite minimalistic:
import os
import redis
r = redis.from_url(os.environ.get("REDIS_URL"))
I'm trying to get this running with the Flask-Session extension. According to their quickstart example:
from flask import Flask, session
from flask.ext.session import Session
app = Flask(__name__)
# Check Configuration section for more details
SESSION_TYPE = 'redis'
app.config.from_object(__name__)
Session(app)
@app.route('/set/')
def set():
session['key'] = 'value'
return 'ok'
I'm confused as to how the two are connected, as the redis module isn't even imported in the latter example. Reading further on the Flask-session page, there's a table of "A list of configuration keys also understood by the extension:", one of which being "SESSION_REDIS", with the description "A redis.Redis instance, default connect to 127.0.0.1:6379". I'm guessing this is the instance (r) from Heroku's docs, but the phrase "keys understood by the extension" gives me no clue regarding what to actually do with it.
Upvotes: 0
Views: 1699
Reputation: 2154
Yes, Flask-session is quite badly documented. keys understood by the extension means you can specify listed configuration options, pass them to your flask app and Flask-session would acknowledge and use those options. It's called keys because flask app accepts configuration in a form of a key-value dictionary.
In Flask-session they use quite unusual approach to configure it: instead of passing such options as host, port, etc. as strings, they require you to pass a configured redis client object:
from flask import Flask, session
from flask_session import Session
from redis import Redis
app = Flask(__name__)
SESSION_TYPE = 'redis'
SESSION_REDIS = Redis(host="your_host", port=1234)
app.config.from_object(__name__)
Session(app)
Upvotes: 1