A simple Flask Redis Tutorial
Install requirements : flask, redis
- Install virtual environment:
sudo apt-get install python3-venv
Creating the python virtual environment:
python3 -m venv venv
Add the root of the repository to the python path upon virtual environment activation:
echo "$(pwd)/src" > redis-venv/lib/python3.5/site-packages/MY_VENV_PYTHONPATH.pth
Activate your python virtual environment:
. venv/bin/activate
Note: To remove the virtual environment from your shell: deactivate
Installing required python modules from newly created virtual environment: Activate your python virtual environment if not already active.
pip install -r requirement.txt
Run the app
python app.py
Flask Redis sample application Git Source Code |
from flask import Flask, jsonify
import redis
import json
app = Flask("redis")
@app.route("/")
def index():
#value to save in redis
value_to_set = "shiva"
#set value to key
redis_db.set('name', value_to_set)
return json.dumps(redis_db.keys())
if __name__ == "__main__":
redis_db = redis.StrictRedis(host="localhost", port=6379, db=0)
app.run(port=15420)
0 comments:
Post a Comment