Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask, TypeError: 'dict' object is not callable

Tags:

python

json

flask

Having an issue that seems to be common yet I have done my research and don't see it being exactly recreated anywhere. When I print json.loads(rety.text), I am seeing the output I need. Yet when I call return, it shows me this error. Any ideas? Help is greatly appreciated and thank you. I am using the Flask MethodHandler.

class MHandler(MethodView):
    def get(self):
        handle = ''
        tweetnum = 100

        consumer_token = '' 
        consumer_secret = ''
        access_token = '-'
        access_secret = ''

        auth = tweepy.OAuthHandler(consumer_token,consumer_secret)
        auth.set_access_token(access_token,access_secret)

        api  = tweepy.API(auth)

        statuses = api.user_timeline(screen_name=handle,
                          count= tweetnum,
                          include_rts=False)

        pi_content_items_array = map(convert_status_to_pi_content_item, statuses)
        pi_content_items = { 'contentItems' : pi_content_items_array }

        saveFile = open("static/public/text/en.txt",'a') 
        for s in pi_content_items_array: 
            stat = s['content'].encode('utf-8')
            print stat

            trat = ''.join(i for i in stat if ord(i)<128)
            print trat
            saveFile.write(trat.encode('utf-8')+'\n'+'\n')

        try:
            contentFile = open("static/public/text/en.txt", "r")
            fr = contentFile.read()
        except Exception as e:
            print "ERROR: couldn't read text file: %s" % e
        finally:
            contentFile.close()
        return lookup.get_template("newin.html").render(content=fr) 

    def post(self):
        try:
            contentFile = open("static/public/text/en.txt", "r")
            fd = contentFile.read()
        except Exception as e:
            print "ERROR: couldn't read text file: %s" % e
        finally:
                contentFile.close()
        rety = requests.post('https://gateway.watsonplatform.net/personality-insights/api/v2/profile', 
                auth=('---', ''),
                headers = {"content-type": "text/plain"},
                data=fd
            )

        print json.loads(rety.text)
        return json.loads(rety.text)


    user_view = MHandler.as_view('user_api')
    app.add_url_rule('/results2', view_func=user_view, methods=['GET',])
    app.add_url_rule('/results2', view_func=user_view, methods=['POST',])

Here is the Traceback(Keep in mind results are printing above):

Traceback (most recent call last):
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1577, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/werkzeug/wrappers.py", line 841, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/werkzeug/test.py", line 867, in run_wsgi_app
    app_rv = app(environ, start_response)
like image 255
puhtiprince Avatar asked Dec 03 '15 04:12

puhtiprince


People also ask

How do I fix dict object is not callable in Python?

The Python "TypeError: 'dict' object is not callable" occurs when we try to call a dictionary as if it were a function. To solve the error, make sure to use square brackets when accessing a key in a dictionary, e.g. my_dict['my_key'] .

Why is Dict not callable?

A Python dictionary contains key-value pairs and uses square brackets to access a value inside the dictionary. If you use curly brackets instead of square brackets, you will get the TypeError: 'dict' object is not callable error.

Why is my dictionary not callable Python?

The “TypeError: 'dict' object is not callable” error is raised when you try to use curly brackets to access items from inside a dictionary. To solve this error, make sure you use the proper square bracket syntax when you try to access a dictionary item.


3 Answers

Flask only expects views to return a response-like object. This means a Response, a string, or a tuple describing the body, code, and headers. You are returning a dict, which is not one of those things. Since you're returning JSON, return a response with the JSON string in the body and a content type of application/json.

return app.response_class(rety.content, content_type='application/json')

In your example, you already have a JSON string, the content returned by the request you made. However, if you want to convert a Python structure to a JSON response, use jsonify:

data = {'name': 'davidism'}
return jsonify(data)

Behind the scenes, Flask is a WSGI application, which expects to pass around callable objects, which is why you get that specific error: a dict isn't callable and Flask doesn't know how to turn it into something that is.

like image 154
davidism Avatar answered Oct 17 '22 15:10

davidism


Use the Flask.jsonify function to return the data.

from flask import jsonify 
# ...
return jsonify(data)
like image 27
Sahil Shah Avatar answered Oct 17 '22 14:10

Sahil Shah


If you return a data, status, headers tuple from a Flask view, Flask currently ignores the status code and content_type header when the data is already a response object, such as what jsonify returns.

This doesn't set the content-type header:

headers = {
    "Content-Type": "application/octet-stream",
    "Content-Disposition": "attachment; filename=foobar.json"
}
return jsonify({"foo": "bar"}), 200, headers

Instead, use flask.json.dumps to generate the data (which is what jsonfiy uses internally).

from flask import json

headers = {
    "Content-Type": "application/octet-stream",
    "Content-Disposition": "attachment; filename=foobar.json"
}
return json.dumps({"foo": "bar"}), 200, headers

Or work with the response object:

response = jsonify({"foo": "bar"})
response.headers.set("Content-Type", "application/octet-stream")
return response

However, if you want to literally do what these examples show and serve JSON data as a download, use send_file instead.

from io import BytesIO
from flask import json
data = BytesIO(json.dumps(data))
return send_file(data, mimetype="application/json", as_attachment=True, attachment_filename="data.json")
like image 6
firelynx Avatar answered Oct 17 '22 14:10

firelynx