gpt4 book ai didi

python - 使用 style_data_conditionals 的 Dash DataTable 单独突出显示效果不寻常

转载 作者:行者123 更新时间:2023-12-04 13:16:17 25 4
gpt4 key购买 nike

我正在使用 Python3、Flask 和 Dash 开发一个项目。
我正在使用 DataTable() 可视化一个 CSV 表来自 dash_table并想突出显示一些特定的单元格。
Accordistrong textng 表格样式的文档,这可以通过使用 style_data_conditional 来完成。 DataTable 定义中的属性 ( reference )。
我的 CSV 表如下所示:

testclient, 0.40, 0.48, False, False, False, 0.14, True, True, 0.0, 2
raspberrypi, 0.20, 0.21, False, True, False, 0.18, True, False, 0.0, 3
尝试访问第一列时,所有样式更改都有效。
[...]
style_data_conditional=[
{
'if': {
'column_id': 'hostname',
'filter_query': '{hostname} eq "testclient"'
},
'color': 'green',
}
],
[...]
但是当尝试访问任何其他行列(如“ftp”或“http”)时,它不起作用,即使我使用 debug=True app.run(...) 处的参数函数调用,我没有得到错误输出。
[...]
style_data_conditional=[
{
'if': {
'column_id': 'ftp',
'filter_query': '{ftp} eq "True"'
},
'color': 'green',
}
],
[...]
DataTable 中有一个“样式”属性的顺序。 ...
  1. style_data_conditional
  2. style_data
  3. style_filter_conditional
  4. style_filter
  5. style_header_conditional
  6. style_header
  7. style_cell_conditional
  8. style_cell

...但正如您所看到的,给定的样式属性是 list 中第一个提到的。
该表定义如下:
content = dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
[...]
你有什么线索,为什么数据表只是通过改变 column_id 表现得那么奇怪? ?

最佳答案

由于您没有在 csv 示例中提供列标题,我只能假设 ftp 和 http 指的是 bool 列?

Dash DataTables 中对 bool 类型列的支持似乎介于有限和不存在之间。 filter_query表达式似乎不适用于 bool 值。 documentation甚至没有提到列的 bool 数据类型:

columns (dict; optional):

  • type (a value equal to: 'any', 'numeric', 'text', 'datetime'; optional): The data-type of the column's data.


我通过将所有 bool 列的数据类型设置为 str 来解决这个问题。在我的数据框中:
for col, dtype in df.dtypes.items():
if dtype == 'bool':
df[col] = df[col].astype('str')

然后,条件样式按预期工作:
DataTable(
[...],
style_data_conditional=[
{
'if': {
'column_id': 'hostname',
'filter_query': '{hostname} eq "testclient"'
},
'backgroundColor': 'green',
},
{
'if': {
'column_id': 'ftp',
'filter_query': '{ftp} eq "True"'
},
'backgroundColor': 'green',
}
]
)

conditional cell styling for Dash DataTable

关于python - 使用 style_data_conditionals 的 Dash DataTable 单独突出显示效果不寻常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60125805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com