Skip to content

Instantly share code, notes, and snippets.

@craigderington
Created September 12, 2019 21:40
Show Gist options
  • Save craigderington/db102287f110f8d75d711001b7d12ec9 to your computer and use it in GitHub Desktop.
Save craigderington/db102287f110f8d75d711001b7d12ec9 to your computer and use it in GitHub Desktop.
Python + Flask + Redis - Page Hit Counter
#!/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
)
@a-mouk
Copy link

a-mouk commented Jun 9, 2020

Hello. Where does "hits" come from? Is it a reserved keyword?

@craigderington
Copy link
Author

Hello. "hits" is the redis "key" in the database. You are incrementing a key pair value.

@viscount-monty
Copy link

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