python - 如果未选择任何选项,则短跑回调
问题描述
如果选择了一个选项,我已经创建了一个回调函数来返回一个过滤的数据表。
# call back to update overview player table
@app.callback(
Output(component_id='player_table', component_property='children'),
[Input(component_id='select_list', component_property='value')]
)
def display_player_table(list_selected):
dff_player = df_player[df_player.band==list_selected]
dff_player.drop(columns=['band',], inplace=True)
return [
dash_table.DataTable(
columns=[{'name': i, 'id': i} for i in dff_player.columns],
data=dff_player.to_dict('records'),
style_header=
# {'display': 'none'},
{
'backgroundColor': 'white',
'fontWeight': 'bold',
'color': 'black',
'border': '0px',
'font_family': 'Roboto',
'font_size' : '18px',
'whiteSpace': 'normal',
},
style_cell={'border': '0px', 'textAlign': 'left', 'font_family': 'Roboto', 'fontWeight': '60', 'font_size' : '15px', 'line_height': '40px', 'width':'60px'},
style_cell_conditional=[
{
'if': {'column_id': c},
'font_size': '11px'
} for c in ['Current Team']
],
style_as_list_view=True,
page_action='native',
fixed_rows={'headers':True},
style_table={'height': '700px', 'overflowY': 'auto'},
)]
我尝试了多种不同的方式(if / else 语句),但如果未选择任何选项,则找不到显示完整 df 的方法。如果未选择任何选项,返回未过滤表的最佳方法是什么?
我正在使用 Python 和 Dash-Plotly
先感谢您
解决方案
你有没有尝试过?
if not list_selected:
dff_player = df_player
else:
dff_player = df_player[df_player.band==list_selected]
另外我不确定 df_player 来自哪里,是否被定义为全局?