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 return a JSON response?
JavaScript
x
5
1
@app.route("/summary")
2
def summary():
3
d = make_summary()
4
# send it back as json
5
Advertisement
Answer
A view can directly return a Python dict or list and Flask will call jsonify
automatically.
JavaScript
1
5
1
@app.route("/summary")
2
def summary():
3
d = make_summary()
4
return d
5
For older Flask versions, or to return a different JSON-serializable object, import and use jsonify
.
JavaScript
1
7
1
from flask import jsonify
2
3
@app.route("/summary")
4
def summary():
5
d = make_summary()
6
return jsonify(d)
7