Return JSON response from Flask view

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

    Pass keyword arguments to flask.jsonify and they will be output as a JSON object.

    @app.route('/_get_current_user')
    def get_current_user():
        return jsonify(
            username=g.user.username,
            email=g.user.email,
            id=g.user.id
        )
    
    {
        "username": "admin",
        "email": "admin@localhost",
        "id": 42
    }
    

    If you already have a dict, you can pass it directly as jsonify(d).

提交回复
热议问题