plotly / plotly/dash

plotly figure subplot grid lost after converting back from JSON

Open
#2,445 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P3
Dominant language
Python
Stars
24.4k
Forks
2.3k
Avg merge
2d 7h
Merged PRs (30d)
13

Description

Describe your context

dash==2.8.1
dash-bootstrap-components==1.4.0
dash-core-components==2.0.0
dash-extensions==0.1.13
dash-html-components==2.0.0
dash-leaflet==0.1.23
dash-table==5.0.0
plotly==5.13.1

Describe the bug
I am trying to update a plotly figure containing subplots within a dash callback but get the error "Exception: Use plotly.tools.make_subplots to create a subplot grid".

The Figure object is passed as a State to the callback function. Because the figure is passed to the callback function as a dict, I use fig =pio.from_json(json.dumps(fig)) to convert it back to a proper Figure object. However, after conversion, this Figure object does not seem to know about its subplots anymore, raising the following exception whenever I try to reference anything subplot-related, e.g. fig.print_grid() or fig.add_trace([...], row=2, col=1):

Exception: Use plotly.tools.make_subplots to create a subplot grid

Minimal code to reproduce:

fig = plotly.subplots.make_subplots(
    rows=3, 
    cols=1,
)
fig.print_grid()     # this works fine
fig2 = pio.from_json(fig.to_json()) # convert to json and back
fig2.print_grid()   # this raises an Exception

Full example app:

import json
from dash import Dash, html, dcc, Input, Output, State, callback_context
import plotly.graph_objects as go
import plotly.io as pio
import plotly.subplots

app = Dash(
    name=__name__,
    title="Test app",
)

# create the initial empty figure
fig = plotly.subplots.make_subplots(
    rows=3,
    cols=1,
)

# create the layout
app.layout = html.Div(children=[
    html.Button("Test me", id="button"),
    dcc.Graph(id="graph", figure=fig)
])


@app.callback(
    Output("graph", "figure"), 
    Input("button", "n_clicks"), 
    State("graph", "figure"),
    prevent_initial_call=True
)
def update_graph(n_clicks, figure):
    # update the figure
    figure = pio.from_json(json.dumps(figure))
    
    figure.print_grid() # this raises an Exception
    
    figure.add_trace(
        go.Scatter(
            x=[0, 1, 2, 3],
            y=[10, 20, 10, 0],
            name=f"line",
        ),
        row=2,
        col=1,
    ) # this also raises an Exception

    return figure


if __name__ == "__main__":
    app.run_server(debug=True)

Expected behavior
I expect to be able to access the subplots of the figure in the callback method.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproduction in the issue, especially make_subplots(), pio.from_json(), and the Dash callback that receives figure state. Trace how subplot information is represented through JSON conversion, then verify the fix by ensuring print_grid() and add_trace(..., row=2, col=1) work after conversion.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.