Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

figure as state out of sync #879

@nicolaskruchten

Description

@nicolaskruchten

In this simple app from @emmanuelle, figure.layout.shapes doesn't seem to be updated when figure is captured as State, meaning that we have to do all sorts of gymnastics with relayoutData to work with shapes.

@alexcjohnson can you help me figure out if this is something in dcc or if Plotly.js is doing something odd here, in which case help me translate this into a CodePen that @archmoj can use?

import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objects as go
from dash.dependencies import Input, Output, State
import dash
import json

app = dash.Dash(__name__)

fig = go.Figure().update_layout(dragmode='drawrect')

app.layout = html.Div([
    dcc.Graph(id='graph', figure=fig),
    html.Pre(id='annotations-data'),
])

@app.callback(
    Output('annotations-data', 'children'),
    [Input("graph", "relayoutData")],
    [State("graph", "figure")]
)
def on_new_annotation(relayout_data, fig):
    return json.dumps(go.Figure(fig).layout.shapes, indent=2)

app.run_server(debug=True)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions