Reputation: 101
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!!
Upvotes: 2
Views: 785
Reputation: 1312
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'))
Upvotes: 1
Reputation: 846
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
Upvotes: 2