使用 style_data_conditionals 的 Dash DataTable 个人突出显示工作异常

Dash DataTable individual highlight using style_data_conditionals works unusual

我正在使用 Python3、Flask 和 Dash 开发一个项目。 我正在使用 dash_table 中的 DataTable() 可视化 CSV Table,并想突出显示一些特定的单元格。

根据 table 样式的文档,这可以通过使用数据 Table 定义 (reference) 中的 style_data_conditional 属性来完成。

我的 CSV table 看起来像这样:

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”时,它不会工作,即使我在 [=19= 处使用 debug=True 参数] 函数调用,我没有得到错误输出。

[...]
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

...但是如您所见,给定的样式属性是列表中第一个提到的。

table的定义是这样的:

content = dash_table.DataTable(
    id='table',
    columns=[{"name": i, "id": i} for i in df.columns],
    [...]

你知道为什么 DataTable 仅仅通过改变 column_id 就表现得那么奇怪吗?

由于您没有在 csv 示例中提供列 headers,我只能假设 ftphttp 引用布尔列?

Dash DataTables 中对 boolean-type 列的支持似乎介于有限和 non-existent 之间。 filter_query 表达式似乎不适用于布尔值。 documentation 甚至没有提到列的布尔数据类型:

columns (dict; optional):

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

我通过在我的数据帧中将所有布尔列的数据类型设置为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',
        }
    ]
)