如果我在破折号中初始化过滤器,如何获得初始数据表?(回调问题)

How to have initial data table if I initialize the filter in dash ? (callback problem)

提问人:stat_man 提问时间:9/20/2023 最后编辑:Dmitrystat_man 更新时间:9/23/2023 访问量:46

问:

我希望在重置过滤器时有一个初始数据表。

这是初始状态。

enter image description here

如果我应用过滤器,它会给我这样的结果:

enter image description here

但是在那之后,如果我删除(或重置)过滤器,它什么也没给我:

enter image description here

这是我的代码。我认为给出整个代码不是很有用,因为它有点长。

@app.callback(
    Output('update-rowdata-grid', 'rowData'),
    Input({'type': 'filter_cat',"table":ALL ,'index': ALL}, 'value'),
    # Input({'type': 'filter_num', 'index': MATCH}, 'value'),
    # Input({'type': 'filter_date', 'index': MATCH}, 'value')
    State('filter_table', 'value'),
    State('second_filter','value'),
    
)
def apply_filter(cat,selected_table,selected_columns):
    df = get_selected_dataframe(selected_table)
    dff = df.copy()
    column_type = df[selected_columns].dtype
    if column_type == 'object' and cat:
        dff = dff[dff[selected_columns].isin(cat[0])]
    else:
        return df.to_dict('records')
        

    return dff.to_dict('records')

我认为我们可以使用一些 If-elif 技巧,但我不知道从哪里开始。

你可以帮我吗?

python if-statement 回调 plotly-dash

评论


答:

1赞 Dmitry 9/23/2023 #1

鉴于以下问题的上下文,我怀疑存在相同的问题:基于 .一旦删除,它至少可以在类别上工作。second_filter

@app.callback(
    Output('update-rowdata-grid', 'rowData', allow_duplicate=True),
    Input({'type': 'filter_cat', "table": ALL, 'index': ALL}, 'value'),
    State({'type': 'filter_cat', "table": ALL, 'index': ALL}, 'id'),
    State('filter_table', 'value'),
    prevent_initial_call=True)
def apply_filter(cat, filter_ids, selected_table):

    dff = get_selected_dataframe(selected_table).copy()

    for filter_id, filter_value in zip(filter_ids, cat):
        if filter_value:
            dff = dff[dff[filter_id['index']].isin(filter_value)]

    return dff.to_dict('records')