Return all data from a loop (multiple returns so the page gets overwritten)
<p>I'm working on an API for an app, but I want to output multiple json objects from my sql query but if I use multiple returns the page gets overwritten.</p>

<p>My code is :</p>

<pre><code>@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})
</code></pre>

<p>How would you fix it? I'm really stuck.</p>

<p>Thanks in advance,</p>

<p>Jordy</p>

<p>I had to use stream_with_context &amp; Response so I could yield it</p>

<p>example:</p>

<pre><code>@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()))
</code></pre>

python flask