Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Json dictionnary on Redis
#1
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:

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 given
Thanks a lot!
K.
Reply


Messages In This Thread
Json dictionnary on Redis - by katsu707 - Dec-04-2018, 08:38 AM
RE: Json dictionnary on Redis - by DeaD_EyE - Dec-04-2018, 11:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionnary indexing error Ander 6 1,964 Sep-10-2021, 12:22 PM
Last Post: Ander
  python dictionnary Omar_Yatim 3 2,888 Dec-08-2019, 05:12 AM
Last Post: scidam
  Dictionnary brackets issue Reldaing 1 1,879 Nov-10-2019, 11:54 PM
Last Post: ichabod801
  Jython macro which uses PythonInterpreter and Redis script gives an error rkanumola 2 2,299 Oct-30-2019, 07:37 AM
Last Post: rkanumola
  Access to the elements of a dictionnary Reims 1 1,661 Oct-02-2019, 12:48 PM
Last Post: SheeppOSU
  from Json Time Serie file to python dictionnary Reims 1 2,076 Sep-11-2019, 08:17 AM
Last Post: DeaD_EyE
  convert a json file to a python dictionnary of array Reims 2 2,285 Sep-10-2019, 01:08 PM
Last Post: Reims
  Receive data from Redis RQ worker process freak14 0 1,934 Jul-15-2019, 12:39 PM
Last Post: freak14
  dictionnary lateublegende 1 2,479 Apr-29-2019, 09:10 PM
Last Post: Yoriz
  Why do we need setdefault() method for dictionnary? DJ_Qu 3 2,735 Apr-21-2019, 11:00 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020