How to update a Plotly graph after receiving image data via POST request?

Viewed 257

I want to use a Dash app to display images that are uploaded via POST-request from a remote client. I'm using a Flask route to handle the POST request:

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"

My question is: Is there a way to update the Graph component right after receiving the image data from within upload_image()? I know I could use an Interval component but I would prefer to trigger the update instantly.

0 Answers
Related