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?
@app.route("/summary") def summary(): d = make_summary() # send it back as json
As of Flask 1.1.0 a view can directly return a Python dict and Flask will call
@app.route("/summary") def summary(): d = make_summary() return d
If your Flask version is less than 1.1.0 or to return a different JSON-serializable object, import and use
from flask import jsonify @app.route("/summary") def summary(): d = make_summary() return jsonify(d)
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
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
Why this Book? Hadoop has been the base for most of the emerging technologies in today’s big data world. It changed the face of distributed processing by using commodity hardware for large data set...
This encyclopedia will be an indispensable resource for our time as it reflects the fact that we are currently living in an expanding data-driven world. ...
Since you are interested in learning more about designing data visualizations (by virtue of the fact that you’re reading this book), then chances are good that you have been the reader of other peop...
Roger Jennings is an author and consultant specializing in Microsoft .NET n-tier database applications and data-intensive Windows Communication Foundation (WCF) Web services with SQL Server. He’s be...