python傳遞list給前端並在前端成功顯示圖片·可以顯示多張後臺傳過來的圖片

python端


#傳遞resultant給html
@app.route('/resultShapeNchange')
def shapeNchange():
    resultant = searchShapeNchange.return_img_stream("./static/photo/test.jpg")
    #searchSapeNchange返回的結果,也就是resultant存放的數據是['/static/airplanes/image_0014.jpg', '/static/airplanes/image_0016.jpg', '/static/airplanes/image_0018.jpg', '/static/airplanes/image_0021.jpg', '/static/airplanes/image_0048.jpg']
    return render_template('resultShapeNchange.html', resultant = resultant)

對應的resultShapeNchange.html,不能直接通過

<img src="{{ resultant[0] }}" />

來接受數據,這樣在前端會顯示在這裏插入圖片描述
而是要根據http://127.0.0.1:5000/來給圖片url

    <img src="{{ 'http://127.0.0.1:5000'+resultant[0] }}" />

此時圖片顯示正常
在這裏插入圖片描述

顯示多張圖片:

    <img src="{{ 'http://127.0.0.1:5000'+resultant[0] }}" />
    <img src="{{ 'http://127.0.0.1:5000'+resultant[1] }}" />
    <img src="{{ 'http://127.0.0.1:5000'+resultant[2] }}" />
    <img src="{{ 'http://127.0.0.1:5000'+resultant[3] }}" />

在這裏插入圖片描述

總結:向前端返回的圖片地址是服務器所在的地址,也就是前端界面上的端口,而直接通過後臺的地址會導致圖片顯示錯誤

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章