Created
September 12, 2019 21:40
-
-
Save craigderington/db102287f110f8d75d711001b7d12ec9 to your computer and use it in GitHub Desktop.
Python + Flask + Redis - Page Hit Counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from flask import Flask | |
from redis import Redis | |
app = Flask(__name__) | |
redis = Redis(host="172.17.0.2", port=6379) | |
@app.route("/", methods=["GET"]) | |
@app.route("/index", methods=["GET", "POST"]) | |
def index(): | |
""" Return the homepage counter """ | |
counter = redis.incr("hits") | |
return render_template( | |
"index.html", | |
counter=counter | |
) | |
if __name__ == "__main__": | |
app.run( | |
host="0.0.0.0", | |
port=8000, | |
debug=True | |
) |
Hello. "hits" is the redis "key" in the database. You are incrementing a key pair value.
Just what I was looking for, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello. Where does "hits" come from? Is it a reserved keyword?