返回Figure Dict时回调返回值无效



我试图返回条形图,但用于条形图的列会根据下拉值的选择而更改。我正在将字典返回到图形输出,但收到以下错误;

dash.exceptions.InvalidCallbackReturnValue:
The callback for property figure``
of component graph-one` returned a value`    
which is not JSON serializable.        
In general, Dash properties can only be    
dash components, strings, dictionaries, numbers, None,    
or lists of those.

不过,我的返回值是字典。下面的代码;

def create_grouped_bar(df):
return [    
go.Bar(
x=df['date'],    
y=df[col],    
text=df[col],    
hoverinfo='text+name',    
name=col.title(),    
)
for col in df.columns
]    
@app.callback(    
Output('graph-one', 'figure'),   
[Input('filtered_df', 'children'),    
Input('freq-radio', 'value'),    
Input('graph-one-drop', 'value'),    
Input('graph-one-radio', 'value')])    
def update_graph(df_json, freq, cols, amount):    
dff = pd.read_json(df_json, orient='split')    
plot_cols = falls_dd_dict[cols]
plot_df = dff[['date_time_occurred']+plot_cols].copy()
plot_df['date_time_occurred'] =           
pd.to_datetime(plot_df.date_time_occurred)
plot_df['date'] = plot_df['date_time_occurred'].dt.to_period(freq)
plot_df = plot_df.groupby(['date']).count()
if amount != 'all':
included_cols = plot_df.sum().sort_values(ascending=False)   [:int(amount)].index.tolist()
plot_df = plot_df[included_cols]
bars = create_grouped_bar(plot_df.reset_index())
figure = {'data': bars,
'layout': go.Layout(    
title='Changes in Enrollment',
margin={'pad': 3, 'l': 45, 'r': 35, 't': 55, 'b': 25},    
barmode='group',    
xaxis={    
'title': 'Month',    
'showgrid': False,    
'showline': True,    
},   
yaxis={    
'title': 'Census',          
'showgrid': False,    
},    
legend=dict(orientation="h", y=-0.15)    
)        
}    
return figure

当我打印图形时,它看起来是这样的;

{'data': [Bar({
'hoverinfo': 'text+name',
'name': 'Date',
'text': array(['2017-12', '2018-01', '2018-02', '2018-03', '2018-04', '2018-05',
'2018-06', '2018-07', '2018-08', '2018-09'], dtype='<U7'),
'x': array([Period('2017-12', 'M'), Period('2018-01', 'M'), Period('2018-02', 'M'),
Period('2018-03', 'M'), Period('2018-04', 'M'), Period('2018-05', 'M'),
Period('2018-06', 'M'), Period('2018-07', 'M'), Period('2018-08', 'M'),
Period('2018-09', 'M')], dtype=object),
'y': array([Period('2017-12', 'M'), Period('2018-01', 'M'), Period('2018-02', 'M'),
Period('2018-03', 'M'), Period('2018-04', 'M'), Period('2018-05', 'M'),
Period('2018-06', 'M'), Period('2018-07', 'M'), Period('2018-08', 'M'),
Period('2018-09', 'M')], dtype=object)
}), Bar({
'hoverinfo': 'text+name',
'name': 'Location',
'text': array(['29', '37', '39', '28', '22', '29', '40', '24', '43', '29'], dtype='<U2'),
'x': array([Period('2017-12', 'M'), Period('2018-01', 'M'), Period('2018-02', 'M'),
Period('2018-03', 'M'), Period('2018-04', 'M'), Period('2018-05', 'M'),
Period('2018-06', 'M'), Period('2018-07', 'M'), Period('2018-08', 'M'),
Period('2018-09', 'M')], dtype=object),
'y': array([29, 37, 39, 28, 22, 29, 40, 24, 43, 29])
}), Bar({
'hoverinfo': 'text+name',
'name': 'Date_Time_Occurred',
'text': array(['29', '37', '39', '28', '22', '29', '40', '24', '43', '29'], dtype='<U2'),
'x': array([Period('2017-12', 'M'), Period('2018-01', 'M'), Period('2018-02', 'M'),
Period('2018-03', 'M'), Period('2018-04', 'M'), Period('2018-05', 'M'),
Period('2018-06', 'M'), Period('2018-07', 'M'), Period('2018-08', 'M'),
Period('2018-09', 'M')], dtype=object),
'y': array([29, 37, 39, 28, 22, 29, 40, 24, 43, 29])
})], 'layout': Layout({
'barmode': 'group',
'legend': {'orientation': 'h', 'y': -0.15},
'margin': {'b': 25, 'l': 45, 'pad': 3, 'r': 35, 't': 55},
'title': 'Changes in Enrollment',
'xaxis': {'showgrid': False, 'showline': True, 'title': 'Month'},
'yaxis': {'showgrid': False, 'title': 'Census'}
})}

我几乎尝试了plotly api的所有变体,包括;

返回一个实际的dcc。将对象图形化为html的子对象。Div,将条形图列表设置为跟踪字典列表,而不是go。Bar((和susing dict(data=bars,layout=layout(,而不是上面的字典。

我也有类似的东西。我花了很长时间才意识到,在我的情况下,我的更新函数需要输出两个列表元素,因为我有两个output((。在你的情况下,我的直觉是,问题可能出在INPUT一侧,你有4个值,其中一个值的ID可能有拼写错误吗(,OUTPUT很好。

最新更新