Return JSON response from Flask view

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

    """ Using Flask Class-base View """

    from flask import Flask, request, jsonify
    
    from flask.views import MethodView
    
    app = Flask(**__name__**)
    
    app.add_url_rule('/summary/', view_func=Summary.as_view('summary'))
    
    class Summary(MethodView):
    
        def __init__(self):
            self.response = dict()
    
        def get(self):
            self.response['summary'] = make_summary()  # make_summary is a method to calculate the summary.
            return jsonify(self.response)
    

提交回复
热议问题