我有一个内置的仪表板。我想增加更多的互动性。我想允许用户在下拉菜单上选择一个选项,并根据上述选择过滤数据表中显示的数据
这就是我定义数据表的方式
html.Div([
dash_table.DataTable(
id='punchstats',
columns=[{'name': i, 'id': i} for i in punch_stats.columns],
data = punch_stats.to_dict('records'),
page_current=0,
page_size=2,
page_action='custom',
sort_action='custom',
sort_mode='multi',
style_table={'overflowX':'scroll',
'maxHeight':'300px'},
style_header={'backgroundColor':'rgb(30, 30, 30)'},
style_cell={'backgroundColor':'rgb(50,50,50)',
'color':'white'},
sort_by=[]),
])
这些是我定义的下拉过滤器
dcc.Dropdown(
id='weight_class',
options=[{'label': i, 'value': i} for i in data['division'].unique()],
multi=True
),
dcc.Dropdown(
id='gender',
options=[{'label': i, 'value':i} for i in fight_outcomes['sex'].unique()],
multi=True
),
这是应该根据下拉菜单选择更新我的表的功能
@app.callback(
dash.dependencies.Output('punchstats','data'),
[dash.dependencies.Input('punchstats','page_current'),
dash.dependencies.Input('punchstats','page_size'),
dash.dependencies.Input('punchstats','sort_by'),
dash.dependencies.Input('weight_class','value'),
dash.dependencies.Input('gender','value')])
def update_punchstats(page_current,page_size,sort_by,weight_class,gender):
if weight_class is None or weight_class == []:
weight_class == WEIGHT_CLASS
if gender is None or gender == []:
gender == GENDER
punchstatsdf = [(punch_stats['division'].isin(weight_class))]
punchstatsdf = [(punchstatsdf['sex'].isin(gender))]
print(sort_by)
if len(sort_by):
sorted_df = punchstatsdf.sort_values(
[cols['column_id'] for cols in sort_by],
ascending=[
cols['direction'] == 'asc'
for col in sort_by
],
inplace=False
)
else:
sorted_df = punchstatsdf
return sorted_df.iloc[page_current*page_size:(page_current+ 1)*page_size].to_dict('records')
不确定如何为这种类型的问题添加最少的可重现示例,希望函数逻辑有问题,可以由对此软件包有更多经验的人发现。
只是要重申这个问题。当我选择给定的体重类别和/或性别时,数据表不会过滤特定于所选体重类别/性别的数据
更新
通过研究我发现,为了允许用户按列排序,我不得不将sort_action更改为native,因为这意味着我必须自己指定排序动作。但是,我仍然在努力让用户根据下拉选择过滤出现在数据表中的数据。
这是我更新的代码。我还添加了:
html.Div([
dash_table.DataTable(
id='punchstats',
columns=[{'name': i, 'id': i} for i in sorted(punch_stats.columns)],
# data = punch_stats.to_dict('records'),
page_current=0,
page_size=5,
page_action='native',
sort_action='native',
column_selectable="single",
row_selectable="single",
sort_mode='multi',
style_table={'overflowX':'scroll',
'maxHeight':'300px'},
style_header={'backgroundColor':'rgb(30, 30, 30)'},
style_cell={'backgroundColor':'rgb(50,50,50)',
'color':'white'},
sort_by=[]),
])
和:
@app.callback(
dash.dependencies.Output('punchstats','data'),
[dash.dependencies.Input('weight_class','value'),
dash.dependencies.Input('gender','value')])
def update_punchstats(weight_class,gender):
if weight_class is None or weight_class == []:
weight_class == WEIGHT_CLASS
if gender is None or gender == []:
gender == GENDER
punchstatsdf = [(punch_stats['division'].isin(weight_class))]
punchstatsdf = [(punchstatsdf['sex'].isin(gender))]
return [
punchstatsdf.to_dict('records')
]
我还尝试通过返回字典来更新回调函数中的破折号数据表:
def update_punchstats(weight_class,gender):
if weight_class is None or weight_class == []:
weight_class == WEIGHT_CLASS
if gender is None or gender == []:
gender == GENDER
punchstats = [(punch_stats['division'].isin(weight_class))]
punchstats = [(punchstats['sex'].isin(gender))]
return {
'data': punchstats.to_dict("records")
}
但是,这将返回一个空的数据表
这是我正在使用的数据:
path_five = 'https://raw.githubusercontent.com/EmmS21/SpringboardCapstoneBoxingPredictionWebApp/master/boxingdata/punchingstats.csv'
punch_stats = pd.read_csv(path_five)
答案 0 :(得分:0)
我知道了。这就是我所做的。
首先,我更改了这些行:
punchstats = [(punch_stats['division'].isin(weight_class))]
punchstats = [(punchstats['sex'].isin(gender))]
对此:
punchstatsdf = punch_stats[punch_stats['division'].isin(weight_class)]
punchstatsdf = punchstatsdf[punchstatsdf['sex'].isin(gender)]
您的变量分配也有问题。我不知道GENDER
和WEIGHT_CLASS
是什么,但我认为它们是字符串列表。您需要使用单个=
而不是双==
进行分配。我也必须这样做。
最后,我只是做了return punchstatsdf.to_dict('records')
。您无需将其放在列表或字典中。