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
If you don't want to use jsonify
for some reason, you can do what it does manually. Call flask.json.dumps
to create JSON data, then return a response with the application/json
content type.
from flask import json
@app.route('/summary')
def summary():
data = make_summary()
response = app.response_class(
response=json.dumps(data),
mimetype='application/json'
)
return response
flask.json
is distinct from the built-in json
module. It will use the faster simplejson
module if available, and enables various integrations with your Flask app.