Dec-04-2018, 08:38 AM
Hi everyone, I'm a complete beginner and here's my issue:
I have Flask as a webserver, Redis is counting the number of hits on the root. I added a arg "name".
I want to store and return a different count that I wish to link to the "name" var.
I got some help and they advised to use a JSON dictionnary. I'm struggling with the implementation. My code looks like this:
K.
I have Flask as a webserver, Redis is counting the number of hits on the root. I added a arg "name".
I want to store and return a different count that I wish to link to the "name" var.
I got some help and they advised to use a JSON dictionnary. I'm struggling with the implementation. My code looks like this:
from flask import Flask
from redis import Redis
import json
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
@app.route('/')
@app.route('/<name>')
def hello(name):
counts = json.loads(redis.get('hits'),('{}'))
try:
counts[name] += 1
except KeyError:
counts[name] = 1
redis.set('hits', json.dumps(counts))
return 'Hello {} I have been seen {} times.\n'.format(name,counts[name])
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)I keep getting this error and I don't know why...Error: File "/app/app3.py", line 11, in hello
counts = json.loads(redis.get('hits'),('{}'))
TypeError: loads() takes 1 positional argument but 2 were givenThanks a lot!K.
