Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_props does not work with Patch() #3101

Open
avm19 opened this issue Dec 10, 2024 · 3 comments
Open

set_props does not work with Patch() #3101

avm19 opened this issue Dec 10, 2024 · 3 comments
Labels
bug something broken P2 considered for next cycle

Comments

@avm19
Copy link

avm19 commented Dec 10, 2024

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
@yuvashrikarunakaran
Copy link
Contributor

import dash
from dash import dcc, html, Output, Input, ctx, Patch
import plotly.graph_objs as go

app = dash.Dash(name)

app.layout = html.Div([
dcc.Graph(id="example-graph", figure=go.Figure(data=[go.Scatter(x=[0], y=[0])], layout_title_text="Initial Title")),
html.Button("Update Figure", id="update-btn"),
])

@app.callback(
Output("example-graph", "figure"),
Input("update-btn", "n_clicks"),
prevent_initial_call=True
)
def update_figure(n_clicks):
if ctx.triggered_id == "update-btn":
# Update figure partially with Patch
patch = Patch()
patch["layout"]["title"]["text"] = f"Updated Title {n_clicks}"
return patch # Expected to merge but replaces the figure
return dash.no_update

if name == "main":
app.run_server(debug=True)

@gvwilson gvwilson changed the title [BUG] set_props does not work with Patch() set_props does not work with Patch() Dec 10, 2024
@gvwilson gvwilson added bug something broken P2 considered for next cycle labels Dec 10, 2024
@avm19
Copy link
Author

avm19 commented Dec 11, 2024

@gvwilson User https://github.com/yuvashrikarunakaran who posted above is engaging in disruptive behaviour. I am not sure if this is a bot, a person in a mental crisis, or both, but you can see from their profile that they are making trivial commits to many repositories, which have been rejected by every other organisation except for Plotly. You can also read the senseless comments they made in this repository.

May I suggest that you ban this user and enable abuse reports for this repository to streamline resolution of similar issues in the future.

@Coding-with-Adam
Copy link
Contributor

@avm19 thank you for the report.
@yuvashrikarunakaran can you please contact us at within the next 24 hours at [email protected]

We've noticed issues with your contributions and comments that need to be discussed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P2 considered for next cycle
Projects
None yet
Development

No branches or pull requests

4 participants