“TypeError”: 'list' object is not callable flask

后端 未结 1 2063
醉酒成梦
醉酒成梦 2020-12-01 21:00

I am trying to show the list of connected devices in browser using flask. I enabled flask on port 8000:

in server.py:

@server.route(\'/devices\',meth         


        
相关标签:
1条回答
  • 2020-12-01 21:34

    The problem is that your endpoint is returning a list. Flask only likes certain return types. The two that are probably the most common are

    • a Response object
    • a str (along with unicode in Python 2.x)

    You can also return any callable, such as a function.

    If you want to return a list of devices you have a couple of options. You can return the list as a string

    @server.route('/devices')
    def status():
        return ','.join(app.statusOfDevices())
    

    or you if you want to be able to treat each device as a separate value, you can return a JSON response

    from flask.json import jsonify
    
    @server.route('/devices')
    def status():
        return jsonify({'devices': app.statusOfDevices()})
        # an alternative with a complete Response object
        # return flask.Response(jsonify({'devices': app.statusOfDevices()}), mimetype='application/json')
    
    0 讨论(0)
提交回复
热议问题