Skip to content

set_props does not work with Patch() #3101

@avm19

Description

@avm19

Describe the bug

set_props does not work with Patch. I guess, it replaces the Figure object with a Patch object instead of merging them.

As a result, in order to update a figure partially, one has to use old-style Output in server-side callbacks.

I haven't seen anything in the docs that would suggest that this is expected behaviour.

Expected behavior

set_props works with Patch or Figure equally well when updating Graph component's figure property.

Reproduce

Code to reproduce
import numpy as np
import plotly
from plotly import express as px, graph_objects as go, figure_factory as ff
from dash import Dash
from dash import html, dcc, callback, Input, Output, Patch, set_props

def make_figure():
    x = np.arange(6)
    y = np.random.randint(6, size=(10))
    return go.Figure(go.Scatter(x=x, y=y))


fig1 = make_figure()
button1 = html.Button("Output()", id="button1", n_clicks=0)
button2 = html.Button("set_props", id="button2", n_clicks=0)
children = [button1, button2, dcc.Graph(id="fig1", figure=fig1)]


@callback(
    Output("fig1", "figure", allow_duplicate=True),
    Input("button1", "n_clicks"),
    prevent_initial_call=True,
)
def regenerate_1(n_clicks):
    fig1 = make_figure()
    patch = Patch()
    patch["data"] = fig1["data"]
    fig1 = patch  # comment/uncomment this
    return fig1


@callback(
    Input("button2", "n_clicks"),
    prevent_initial_call=True,
)
def regenerate_2(n_clicks):
    fig1 = make_figure()
    patch = Patch()
    patch["data"] = fig1["data"]
    fig1 = patch  # comment/uncomment this
    set_props("fig1", {"figure": fig1})


app = Dash()
app.layout = html.Div(children)
app.run(jupyter_mode="external", port=8051)
`pip list | grep dash` below
dash                      2.18.1
dash-bootstrap-components 1.6.0
dash-core-components      2.0.0
dash-html-components      2.0.0
dash-mantine-components   0.12.1
dash-table                5.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2considered for next cyclebugsomething broken

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions