首页 > 解决方案 > 通过 POST 请求接收图像数据后如何更新 Plotly 图?

问题描述

我想使用 Dash 应用程序显示通过远程客户端的 POST 请求上传的图像。我正在使用 Flask 路由来处理 POST 请求:

from dash import Dash
from flask import request
import dash_core_components as dcc
import numpy as np
import plotly.express as px
import cv2

app = Dash(__name__)
app.layout = dcc.Graph(id="graph")

@app.server.route("/images/upload", methods=["POST"])
def upload_image():
    """Upload an image via post request."""
    if request.method == 'POST':
        # check if the post request has the file part
        data = request.data
        if data:
            nparr = np.fromstring(data, np.uint8)
            img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

            # this is the figure I want to load in the graph
            fig = px.imshow(img)

            # call a callback to update the graph component or update directly
            # ...
            # ...

            return "OK"

我的问题是:有没有办法在从内部接收到图像数据后立即更新 Graph 组件upload_image()?我知道我可以使用 Interval 组件,但我更愿意立即触发更新。

标签: pythonplotly-dash

解决方案


推荐阅读