简体   繁体   中英

Dashboard Plotly ValueError: Invalid value

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!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM