gpt4 book ai didi

Python flask ,类型错误 : 'dict' object is not callable

转载 作者:IT老高 更新时间:2023-10-28 12:48:05 32 4
gpt4 key购买 nike

有一个似乎很常见的问题,但我已经完成了我的研究,并没有看到它在任何地方被完全重现。当我打印 json.loads(rety.text) 时,我看到了我需要的输出。然而,当我调用 return 时,它向我显示了这个错误。有任何想法吗?非常感谢您的帮助,谢谢。我正在使用 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',])

这是 Traceback(请记住结果在上面打印):

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)

最佳答案

Flask only expects views to return a response-like object.这意味着 Response、字符串或描述正文、代码和标题的元组。您正在返回一个字典,这不是其中之一。由于您要返回 JSON,因此返回正文中包含 JSON 字符串和内容类型为 application/json 的响应。

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

在您的示例中,您已经有一个 JSON 字符串,即您发出的请求返回的内容。但是,如果要将 Python 结构转换为 JSON 响应,请使用 jsonify:

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

在幕后,Flask 是一个 WSGI 应用程序,它希望传递可调用对象,这就是为什么你会得到这个特定错误:一个 dict 是不可调用的,而 Flask 不知道如何把它变成.

关于Python flask ,类型错误 : 'dict' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34057851/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com