使用 Flask 顯示的影像錯誤或損壞
P粉299174094
P粉299174094 2023-09-12 00:21:51
0
1
479

我正在設定一個網頁應用程序,我需要在其中顯示一些圖像。我從 API 收到的圖像將 matplotlib 圖轉換為 png,然後使用 io 庫將其發送到主 Web 應用程式。因此,我在頁面上顯示的圖像幾乎總是顯示為不正確或錯誤。但如果我透過上下文選單在新頁面中開啟它們,它們就可以了。

所以這段發送圖表的程式碼

@app.route('/send-data-a', methods=['GET'])
def send_data_user_dynamic():
...some code for diagram...
image_stream1 = io.BytesIO()
        plt.savefig(image_stream1, format='png')
        image_stream1.seek(0)
        plt.close(fig)

        return send_file(image_stream1, mimetype='image/png')

我嘗試將時間戳放在主應用程式中,因此連結將是唯一的,但是沒有幫助

@app.route('/data', methods=['GET'])
def data():
    timestamp = int(time.time())

    user_dynamic = requests.get(f'http://127.0.0.1:5000/send-data-a?timestamp={timestamp}')
    user_amount = requests.get(f'http://127.0.0.1:5000/send-data-b?timestamp={timestamp}')
    kp_month = requests.get(f'http://127.0.0.1:5000/send-data-c?timestamp={timestamp}')
    kp_week = requests.get(f'http://127.0.0.1:5000/send-data-d?timestamp={timestamp}')

    return render_template('second.html',
                           user_dynamic=user_dynamic.url,
                           user_amount=user_amount.url,
                           kp_month=kp_month.url,
                           kp_week=kp_week.url)
if __name__ == '__main__':
    app.run(debug=True, port=5001)

並且有一個 html 模板可以輸出它們

<div class="diagram">
        <img src="{{ user_dynamic }}" alt="user_dynamic">
        <figcaption>some text</figcaption>
    </div>

P粉299174094
P粉299174094

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!