2

I have a Dash app that plots several graphs. When the Dash app starts, some plots do not get displayed, and I see the error. This only occurs on the initial startup of the app. When the webpage is refreshed, the error does not re-appear, and all plots get displayed without errors.

Callback error updating {"index":1,"tag":"bar-9-graph"}.figure

@app.callback(
    ServersideOutput("filtered-data", "data"),
    Input({"tag": "v2", "index": 1}, "value"),
    Input({"tag": "v3", "index": 1}, "value"),
    Input({"tag": "v4", "index": 1}, "value"),
    Input({"tag": "date-range", "index": 1}, "start_date"),
    Input({"tag": "date-range", "index": 1}, "end_date"),
    memoize=True 
)
def filter_data(v2, v3, v4, start_date, end_date):
    data = hc._select_filter(df, labels_dict.keys(), [v2, v3, v4])
    data = hc._date_filter(data, "fecha", start_date, end_date)
    return data
@app.callback(
    Output({"tag": "bar-9-graph", "index": 1}, "figure"),
    Input("filtered-data", "data"),
)
def make_bar_2(data):
    data_aux = data.copy()
    data_aux = data_aux.loc[:,['nit','frequency','group']]
    data_aux = data_aux.drop_duplicates(subset=['nit'])
    data_aux = data_aux.groupby(['frequency'], as_index=False).size()
    return hc.generic_bar_graphB(data_aux, "frequency")  
def generic_bar_graphB(data: pd.Series, column: str,):
    fig = px.bar(data, x=column, y='size', title="", labels={column:''})
    fig.update_xaxes(tickangle = 330)
    fig.update_layout()
    return fig

Thanks!!

2
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented Nov 8, 2022 at 22:33
  • Please copy and paste the entire error message. If you can add small data to understand how we can reproduce your problem and fix it. Commented Nov 9, 2022 at 1:21

2 Answers 2

2

I had a similar issue, but found a solution of sorts from LeoWY on the Plotly forums. LeoWY suggests calling the graph within a try/except block in which you add the same function call after both the try statement and the except statement. As LeoWY explains, this method should allow the graph to render properly the second time if it doesn't render correctly at first.

For instance, you could update your generic_bar_graph function as follows:

def generic_bar_graphB(data: pd.Series, column: str,):
try:
    fig = px.bar(data, x=column, y='size', title="", labels={column:''})
except:
    fig = px.bar(data, x=column, y='size', title="", labels={column:''})
fig.update_xaxes(tickangle = 330)
fig.update_layout()
return fig
Sign up to request clarification or add additional context in comments.

Comments

1

Found better solution at https://github.com/plotly/plotly.py/issues/3441

Could be fixed by adding somewhere in the code before creating plots:

go.Figure(layout=dict(template='plotly'))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.