Return JSON response from Flask view

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

    Flask 1.1.x

    now Flask support request return with json directly, jsonify not required anymore

    @app.route("/")
    def index():
        return {
            "api_stuff": "values",
        }
    

    is equivalent to

    @app.route("/")
    def index():
        return jsonify({
            "api_stuff": "values",
        })
    

    for more information read here https://medium.com/octopus-wealth/returning-json-from-flask-cf4ce6fe9aeb and https://github.com/pallets/flask/pull/3111

提交回复
热议问题