Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
# $ pip install beaker flask redis git+git://github.com/bbangert/beaker_extensions.git
from flask import Flask, session
from flask.sessions import SessionInterface
from beaker.middleware import SessionMiddleware
session_opts = {
'session.type': 'redis',
'session.url': '127.0.0.1:6379',
}
class BeakerSessionInterface(SessionInterface):
def open_session(self, app, request):
return request.environ['beaker.session']
def save_session(self, app, session, response):
session.save()
app = Flask(__name__)
@app.route('/')
def index():
if not session.has_key('value'):
session['value'] = 'Save in session'
return "Session value set."
else:
return session['value']
if __name__ == '__main__':
app.wsgi_app = SessionMiddleware(app.wsgi_app, session_opts)
app.session_interface = BeakerSessionInterface()
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment