[英]Return a json on browser - Flask
I have this function, it returns a result on console: 我有此功能,它在控制台上返回结果:
numbers_to_add = list(range(10000001))
try:
req = request.Request('http://127.0.0.1:5000/total'
, data=bytes(json.dumps(numbers_to_add), 'utf_8')
, headers={'Content-Type': 'application/json'}
, method='POST')
result = json.loads(request.urlopen(req).read(), encoding='utf_8')
print(json.dumps(result, indent=4))
except Exception as ex:
print(ex)
It returns a result on range 10000001
它返回范围为10000001
的结果
Now , I want to return this on browser request, in a Flask application, I've tried this: 现在,我想在浏览器请求中返回它,在Flask应用程序中,我已经尝试过了:
def hardCoded():
numbers_to_add = list(range(10000001))
try:
req = request.Request('http://127.0.0.1:5000/total'
, data=bytes(json.dumps(numbers_to_add), 'utf_8')
, headers={'Content-Type': 'application/json'}
, method='POST')
result = json.loads(request.urlopen(req).read(), encoding='utf_8')
print(json.dumps(result, indent=4))
except Exception as ex:
print(ex)
class rangeNumbers(Resource):
def get(self, range):
return {'data': directSum.hardCoded(range)}
api.add_resource(rangeNumbers, '/range/<range>')
When I query this on my browser, it throws me this: 当我在浏览器中查询此消息时,它会抛出以下消息:
Traceback (most recent call last):
File "/home/user/.virtualenvs/test_sum/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/home/user/.virtualenvs/test_sum/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/user/.virtualenvs/test_sum/lib/python3.6/site-packages/flask_restful/__init__.py", line 480, in wrapper
resp = resource(*args, **kwargs)
File "/home/user/.virtualenvs/test_sum/lib/python3.6/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/home/user/.virtualenvs/test_sum/lib/python3.6/site-packages/flask_restful/__init__.py", line 595, in dispatch_request
resp = meth(*args, **kwargs)
File "app.py", line 16, in get
return {'data': directSum.hardCoded()}
TypeError: hardCoded() takes 0 positional arguments but 1 was given
Any ideas? 有任何想法吗?
If range
is meant to be the n
number to return, in this case, 10000001
, then you will want to do this instead: 如果将range
设为要返回的n
数字(在这种情况下为10000001
,那么您将需要这样做:
In your directSum
file: 在您的directSum
文件中:
def hardCoded(rng):
numbers_to_add = list(range(rng))
try:
# ... rest of code ...
In your main file: 在您的主文件中:
class rangeNumbers(Resource):
def get(self, rng):
return {'data': directSum.hardCoded(rng)}
Where when you call rangeNumbers().get
you do this: 在调用rangeNumbers().get
执行以下操作:
rng_num = rangeNumbers()
rng_num.get(10000001)
Notice I changed your variable range
to rng
. 注意,我将您的变量range
更改为rng
。 It's in your best interest to not overshadow the builtin
names even within a local scope. 您的最大利益就是即使在本地范围内也不要掩盖builtin
名称。 Otherwise calling range(range)
is going to give you endless pain. 否则,调用range(range)
会给您带来无尽的痛苦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.