Flask with Redis



examples/flask-redis/app.py
from flask import Flask, request, render_template
import redis
app = Flask(__name__)

r = redis.Redis(host='my-redis', port=6379, db=0)

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

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

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


examples/flask-redis/templates/red.html
<form action="/save" method="POST">
<input name="field">
<input name="value">
<input type="submit" value="Save">
</form>

<form action="/get" method="POST">
<input name="field">
<input type="submit" value="Get">
</form>

{% if saved %}
<b>saved</b>
{{ value }}
{% endif %}

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



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