Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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

1 Answer

0 votes
by (106k points)

To return the results as a response from a Flask view you can pass the summary data to the jsonify function, which returns a JSON response.

jsonify():-

This function turns the JSON output into a response object with the application/json mime type. For users convenience, it also converts multiple arguments into an array or multiple keyword arguments into a dictionary. So, it means that if we do jsonify(1,2,3) and jsonify([1,2,3]) both are serialize as [1,2,3]. For that, you will write the following piece of code.

from flask import jsonify

@app.route('/summary')

def summary():

   d = make_summary()

   return jsonify(d)

If you wish to learn more about Python, visit Python tutorial and Python course by Intellipaat.

Related questions

0 votes
1 answer
asked Oct 9, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 12, 2020 in Python by laddulakshana (12.7k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

29.3k questions

30.6k answers

501 comments

104k users

Browse Categories

...