Forums

Flask: how to return number, list instead of string?

hi, newbie to Flask, have this prob: basic returns seem to work ok if converted to a string:

ex:

     a = [1,2,3,4,5,6]
 def list1():    
    return str(a[::-1])

this gives me: [6, 5, 4, 3, 2, 1] in the url

but how to just return a list or integer without having to convert to str first?

duplicate post

set up in flask_app.py as

@app.route('/list1')
def list1():
    a = [1,2,3,4,5,6]
    return str(a[::-1])

You can't return a list from a view: HTTP is for moving text around. If you're trying to use it as an endpoint for an API, then you can encode it in some way like JSON and parse that in the code that uses the API.

ok thanks, I'll look into that