You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dash带下拉列表的表格更新异常:清空选择后返回空表

问题描述

使用Dash创建带下拉筛选的表格,当前代码存在异常:选择1个或多个下拉选项时,表格能正常展示对应数据;但清空下拉选择后,预期显示完整表格,实际却返回空表格。

组件代码

#7th row components
bullion_coins_dropdown = dcc.Dropdown(id = 'bullion_coins_dropdown', options = [x for x in sorted(bullion_coins_df.Coin.unique())],
                                      multi = True)

#8th row components
bullion_coins_table = dash_table.DataTable(id = 'bullion_coins_table',
                                           columns = [
                                               {'name' : 'Coin', 'id' : 'Coin', 'type' : 'text'},
                                               {'name' : 'Mint', 'id' : 'Mint', 'type' : 'text'},
                                               {'name' : 'Country', 'id' : 'Country', 'type' : 'text'},
                                               {'name' : 'Weight [g]', 'id' : 'Weight [g]', 'type' : 'numeric'},
                                               {'name' : 'Fineness [%]', 'id' : 'Fineness [%]', 'type' : 'numeric'},
                                               {'name' : 'Diameter [mm]', 'id' : 'Diameter [mm]', 'type' : 'numeric'},
                                               {'name' : 'Diameter tolerance [mm]', 'id' : 'Diameter tolerance [mm]', 'type' : 'numeric'},
                                               {'name' : 'Thickness [mm]', 'id' : 'Thickness [mm]', 'type' : 'numeric'},
                                               {'name' : 'Thickness tol. [mm]', 'id' : 'Thickness tol. [mm]', 'type' : 'numeric'},
                                               {'name' : 'Metal', 'id' : 'Metal', 'type' : 'text'}
                                            ],
                                           data = bullion_coins_df.to_dict('records'),
                                           filter_action = 'native',
                                           page_size = 5,
                                           
                                           style_data = {
                                               'width' : '100px', 'minWidth' : '100px', 'maxWidth' : '100px',
                                               'overflow' : 'hidden',
                                               'textOverflow' : 'ellipsis',
                                           })

回调代码

@app.callback(
    Output(bullion_coins_table, component_property = 'data'),
    Input(bullion_coins_dropdown, component_property = 'value')
)

def table_func(bullion_coins_dropdown):
    if bullion_coins_dropdown == None:
        bullion_coins_table = bullion_coins_df.to_dict('records') 
    else:
        bullion_coins_table = bullion_coins_df[bullion_coins_df.Coin.isin(bullion_coins_dropdown)].to_dict('records')
    return bullion_coins_table
问题原因

开启multi=True的dcc.Dropdown,在清空选择时返回的是空列表[],而非None。原代码仅判断了bullion_coins_dropdown == None的情况,清空下拉时会进入else分支,isin([])会过滤掉所有数据,导致表格为空。

修复方案

修改回调函数的判断条件,同时处理None和空列表的场景:

@app.callback(
    Output(bullion_coins_table, component_property='data'),
    Input(bullion_coins_dropdown, component_property='value')
)
def table_func(selected_coins):
    # 处理未选择或清空选择的情况
    if not selected_coins:
        return bullion_coins_df.to_dict('records')
    # 处理选择了选项的情况
    else:
        filtered_df = bullion_coins_df[bullion_coins_df.Coin.isin(selected_coins)]
        return filtered_df.to_dict('records')

说明:if not selected_coins会同时匹配None和空列表[]的情况,此时直接返回完整数据;当有选择项时,再执行筛选逻辑。

内容的提问来源于stack exchange,提问作者kamil1992

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 15:31:29