返回数字字典时无效的回调返回值

时间:2018-12-07 03:42:08

标签: python plotly plotly-dash

我试图返回一个条形图,但是我用于条形的列会根据下拉值的选择而变化。我将字典返回到graph.figure输出,但是收到以下错误;

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'}

})}

我已经尝试了很多api的变体,包括;

将实际的dcc.Graph对象作为子对象返回到html.Div,将条形列表设置为跟踪词典列表,而不是go.Bar(),而使用dict(data = bars,layout = layout)上面的词典中的内容。

1 个答案:

答案 0 :(得分:0)

我有类似的事情。让我花了很长时间认识到我的情况下,我的update函数需要输出两个列表元素,因为我有两个output()。在您的情况下,我的直觉是问题可能出在INPUT端,您有4个值,其中之一的ID是否有错字,并且OUTPUT很好。