Flask with Redis



examples/flask-redis/app.py
from flask import Flask, request, render_template
import redis

app = Flask(__name__)

red = redis.Redis(host='redis', port=6379, db=0)

@app.route("/")
def main():
    return render_template('red.html')

@app.route("/save", methods=['POST'])
def save():
    field = request.form['field']
    value = request.form['value']
    ret = red.set(field, value)
    app.logger.debug(ret)
    new_value = red.get(field)
    return render_template('red.html', saved=1, value=new_value)

@app.route("/get", methods=['POST'])
def get():
    field = request.form['field']
    value = red.get(field)
    if value is None:
        return render_template('red.html', field=field, value="Not defined yet")
    str_value = value.decode('utf-8')
    return render_template('red.html', field=field, value=str_value)

@app.route("/keys", methods=['GET'])
def keys():
    all_keys = red.keys("*")
    return render_template('red.html', fields=all_keys)

examples/flask-redis/templates/red.html
<h1>Flask + Redis</h1>

<div>
<a href="/">home</a> <a href="/keys">keys</a>
</div>

Type in a key and a value and save it to Redis.
<form action="/save" method="POST">
<input name="field">
<input name="value">
<input type="submit" value="Save">
</form>

Type in a key and fetch the value from Redis.
<form action="/get" method="POST">
<input name="field">
<input type="submit" value="Get">
</form>

{% if saved %}
<b>saved</b>
{{ value.decode('utf8') }}
{% endif %}

{% if field %}
  The value of <b>{{ field }}</b> is <b>{{ value }}</b>
{% endif %}


{% if fields %}
  <h2>Keys</h2>
  <ul>
  {% for field in fields %}
     <li>{{ field.decode('utf8') }}</li>
  {% endfor %}
  </ul>
{% endif %}

examples/flask-redis/requirements.txt
flask
pytest
redis