Return JSON response from Flask view

前端 未结 15 2586
时光取名叫无心
时光取名叫无心 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 05:47

    jsonify serializes the data you pass it to JSON. If you want to serialize the data yourself, do what jsonify does by building a response with status=200 and mimetype='application/json'.

    from flask import json
    
    @app.route('/summary')
    def summary():
        data = make_summary()
        response = app.response_class(
            response=json.dumps(data),
            status=200,
            mimetype='application/json'
        )
        return response
    

提交回复
热议问题