簡體   English   中英

使用 Flask 循環繪制實時數據時出現問題

[英]Trouble with return in loop plotting real time data using Flask

在此代碼中使用列表中的值時出現問題。 Return 語法打破了循環,我可以使用列表中的所有數據。 這是我的代碼:

@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

謝謝

嘗試聲明一個空列表,然后 append 將結果返回到 for 循環中的列表,然后再返回,如下所示:

@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

嘗試這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM