Return JSON response from Flask view

前端 未结 15 2621
时光取名叫无心
时光取名叫无心 2020-11-21 05:11

I have a function that analyzes a CSV file with Pandas and produces a dict with summary information. I want to return the results as a response from a Flask view. How do I r

15条回答
  •  遥遥无期
    2020-11-21 06:06

    To return a JSON response and set a status code you can use make_response:

    from flask import jsonify, make_response
    
    @app.route('/summary')
    def summary():
        d = make_summary()
        return make_response(jsonify(d), 200)
    

    Inspiration taken from this comment in the Flask issue tracker.

提交回复
热议问题