我有一个仪表板我建立。我想增加更多的互动性。我想允许用户在下拉菜单上选择一个选项,我的数据表中显示的数据将根据所述选择进行过滤
这就是我定义数据表的方式
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=[]),
])
这些是我定义的下拉过滤器
^{pr2}$这个函数应该根据下拉选择来更新我的表
@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_操作更改为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')
]
我还尝试通过返回字典来更新回调函数中的dash datatable:
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")
}
但是,这将返回一个空的datatable
以下是我使用的数据:
path_five = 'https://raw.githubusercontent.com/EmmS21/SpringboardCapstoneBoxingPredictionWebApp/master/boxingdata/punchingstats.csv'
punch_stats = pd.read_csv(path_five)
我成功了。这是我所做的。在
首先,我改了这些台词:
为此:
^{pr2}$变量赋值也有问题。我不知道
GENDER
和WEIGHT_CLASS
是什么,但我假设它们是字符串列表。您需要使用一个=
而不是双==
进行赋值。我也必须这么做。在最后,我只是简单地做了
return punchstatsdf.to_dict('records')
。你不需要把它放在一个列表或一个字典里相关问题 更多 >
编程相关推荐