Tei*_*Rei 5 python plotly plotly-dash
我想根据一个值更改Dash dataTable单元格的颜色。我尝试了一个最小的例子:
html.Div(
children=[
dash_table.DataTable(
id='table_1',
data=df.to_dict('records'),
columns=[{"name": i, "id": i} for i in df.columns],
#conditional cell formating
style_data_conditional=[
{
'if': {
'column_id': 'col1',
'filter': 'col1 > num(15)'
},
'backgroundColor': '#3D9970',
'color': 'white',
},
],
n_fixed_rows=2,
filtering=True,
sorting=True,
sorting_type="multi"
)],
)
Run Code Online (Sandbox Code Playgroud)
添加style_conditional之后,该表根本不会显示,并且不会引发任何错误消息。该表嵌入在html组件中,进入论坛和github后,我不确定是否在这里错过了任何内容,以及是否需要为此编写回调函数。提到的教程中提供的示例并不建议需要回调。
更新:
尝试使用相同的代码和不同的数据运行最低版本,并获得相同的结果,即单元格颜色不变。我的库是最新的,但是环境中的某些内容仍可能会引起问题。
完整代码:
import dash
import dash_table
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')
# Having trouble with conditional when column name has spaces
df = df.rename({'Number of Solar Plants': 'Plants'}, axis='columns')
app = dash.Dash(__name__)
app.layout = dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
style_data_conditional=[{
"if": {'column_id': 'State',
'filter': 'State eq "Nevada"'
},
"backgroundColor": "#3D9970",
"color": "white"
}],
data=df.to_dict('records'),
)
if __name__ == '__main__':
app.run_server(debug=True)
Run Code Online (Sandbox Code Playgroud)
我不知道 2019 年情况如何,但最近发布的Dash Datatables:
filter已更名为filter_querystyle_data_conditional=[{
"if": {
'column_id': 'State',
'filter_query': '{State} eq "Nevada"'
# ^ ^ <-- required braces
},
"backgroundColor": "#3D9970",
"color": "white"
}]
Run Code Online (Sandbox Code Playgroud)
顺便说一句:您可以通过删除该行来方便地突出显示整行column_id。
| 归档时间: |
|
| 查看次数: |
1135 次 |
| 最近记录: |