Return JSON response from Flask view

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

    Prior to Flask 0.11, jsonfiy would not allow returning an array directly. Instead, pass the list as a keyword argument.

    @app.route('/get_records')
    def get_records():
        results = [
            {
              "rec_create_date": "12 Jun 2016",
              "rec_dietary_info": "nothing",
              "rec_dob": "01 Apr 1988",
              "rec_first_name": "New",
              "rec_last_name": "Guy",
            },
            {
              "rec_create_date": "1 Apr 2016",
              "rec_dietary_info": "Nut allergy",
              "rec_dob": "01 Feb 1988",
              "rec_first_name": "Old",
              "rec_last_name": "Guy",
            },
        ]
        return jsonify(results=list)
    

提交回复
热议问题