从循环中返回所有数据(多次返回以便页面被覆盖)

时间:2016-09-29 09:21:09

标签: python flask

我正在开发一个应用程序的API,但是我想从我的sql查询中输出多个json对象,但是如果我使用多个返回页面就会被覆盖。

我的代码是:

@app.route('/api/location')
@support_jsonp
def get_locations():
    d = {}
    for i, row in enumerate(locations):
            l = []
            for col in range(0, len(row)):
                    l.append(row[col])
            d[i] = l
            for s in range(0, len(d)):
                    db_questionid = d[s][0]
                    db_title = d[s][1]
                    db_text = d[s][2]
                    db_long = d[s][3]
                    db_lat = d[s][4]
                    db_completed = d[s][5]
                    db_image = d[s][6]
                    return jsonify({'id': db_questionid,
                    'title': db_title,
                    'text': db_text.decode("ISO-8859-1"),
                    'long': db_long,
                    'lat': db_lat,
                    'completed': db_completed,
                    'image': db_image})

你会如何解决?我真的被卡住了。

提前致谢,

的Jordy

1 个答案:

答案 0 :(得分:0)

我不得不使用stream_with_context&回应所以我可以屈服它

示例:

@app.route('/api/location')
@support_jsonp
def get_locations():
       def generate():
            d = {}
            for i, row in enumerate(locations):
                    l = []
                    for col in range(0, len(row)):
                            l.append(row[col])
                    d[i] = l
                    for s in range(0, len(d)):
                            db_questionid = d[s][0]
                            db_title = d[s][1]
                            db_text = d[s][2]
                            db_long = d[s][3]
                            db_lat = d[s][4]
                            db_completed = d[s][5]
                            db_image = d[s][6]
                            yield jsonify({'id': db_questionid,
                            'title': db_title,
                            'text': db_text.decode("ISO-8859-1"),
                            'long': db_long,
                            'lat': db_lat,
                            'completed': db_completed,
                            'image': db_image}).data
          return Response(stream_with_context(generate()))