简体   繁体   中英

Trouble with return in loop plotting real time data using Flask

I have problem when use value from list in this code. Return syntax breaks the loop and I can use all the data from the list. This is my code:

@app.route('/data', methods=["GET", "POST"])
def data():
    value1 = [1, 2, 3, 4, 5, 6]
    for i in range(0,len(value1)):
        data = [time() * 1000, value1[i]]
        response = make_response(json.dumps(data))
        response.content_type = 'application/json'
        return response

Thanks

Try declaring an empty list, and append the results to the list in the for-loop before returning it, like this:

@app.route('/data', methods=["GET", "POST"])
def data():
    value1 = [1, 2, 3, 4, 5, 6]
    response_arr = []
    for i in range(0,len(value1)):
        data = [time() * 1000, value1[i]]
        response = make_response(json.dumps(data))
        response.content_type = 'application/json'
        response_arr.append(response)

    return response_arr

Try something like this:

def data():

    all_data = []
    value1 = [1, 2, 3, 4, 5, 6]

    for number,value in enumerate(value1):
        data = [time() * 1000, value]
        all_data.append(data)

    response = make_response(json.dumps(all_data))
    response.content_type = 'application/json'

    return response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM