13

What is the advantage of using the Flask Redis extension in this example...

from flask import Flask, render_template
from flask.ext.redis import Redis


app = Flask(__app__)
app.config['REDIS_HOST'] = 'localhost'
app.config['REDIS_PORT'] = 6379
app.config['REDIS_DB'] = 0


r = Redis(app)

@app.route("/")
def index():
    return render_template("index.html", **r.hgetall("temp.index"))

...over a regular Redis connection instance?

from flask import Flask, render_template

import redis


r = redis.Redis()

@app.route("/")
def index():
    return render_template("index.html", **r.hgetall("temp.index"))
0

1 Answer 1

16

Current maintainer of the package here, hi!

I think the two (admittedly minor) conveniences that the package provides are:

  1. Integration with Flask's configuration management, so you can organize all your app configuration in the same place. You can add your Redis database URL next to the one for PostgreSQL or whatever you're using, and it'll be picked up automatically when you initialize the FlaskRedis app.

  2. Automatically attaching to your Flask application, so you don't have to keep importing your Redis instance across modules, as it's already always with you, accessible like this: app.extensions['redis']. But if you want to import it anyway, you can also import the module with import flask.ext.redis.

2
  • 2
    NOTE: From the Flask 0.11 docs: "As of Flask 0.11, most Flask extensions have transitioned to the new naming schema. The flask.ext.foo compatibility alias is still in Flask 0.11 but is now deprecated – you should use flask_foo." I'm assuming flask.ext.redis is no exception?
    – franklin
    Commented May 30, 2016 at 20:02
  • @franklin yes, I switched to flask_redis and it is all good
    – Jinsong Li
    Commented Nov 5, 2017 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.