简体   繁体   中英

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

Now , I want to return this on browser request, in a Flask application, I've tried this:

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:

In your directSum file:

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:

rng_num = rangeNumbers()
rng_num.get(10000001)

Notice I changed your variable range to rng . It's in your best interest to not overshadow the builtin names even within a local scope. Otherwise calling range(range) is going to give you endless pain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM