我会推荐下面的,而不是直接使用 Pandas 的样式化组件
- 将您的数据框转换为绘图数据表
- 根据 plotly-dash 文档格式化 dash 数据表。
示例代码如下
mytable = dash_table.DataTable(
id='table1',
columns=[{"name": i, "id": i,'type':'numeric', 'format': {'specifier': ',.0f'}} if i not in ['phone_number'] else {"name": i, "id": i} for i in df.columns],
data=df.to_dict('records'),
style_as_list_view=True,
style_cell={'padding': '2px'},
page_action='none',
style_table={'height': '600px', 'overflowY': 'auto'},
style_cell_conditional=[
{
'if': {'column_id': 'phone_number'},
'textAlign': 'center'
},
],
style_data_conditional=[
{
'if': {'row_index': 0},
'backgroundColor': '#FFF2CC',
},
],
style_header={
'backgroundColor': 'white',
'fontWeight': 'bold',
'textAlign': 'center'
},
)