【问题标题】:Dash DataTable individual highlight using style_data_conditionals works unusual使用 style_data_conditionals 的 Dash DataTable 单独突出显示效果不寻常
【发布时间】:2020-05-24 07:40:13
【问题描述】:

我正在使用 Python3、Flask 和 Dash 开发一个项目。 我正在使用来自dash_tableDataTable() 可视化一个CSV 表,并希望突出显示一些特定的单元格。

Accordistrong 为表格样式的文档提供文本,这可以通过使用 DataTable 定义 (reference) 中的 style_data_conditional 属性来完成。

我的 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”)时,它不起作用,即使我在 app.run(...) 函数调用中使用 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

...但正如您所看到的,给定的样式属性是清单中第一个提到的。

表是这样定义的:

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

你有什么线索吗,为什么仅仅通过更改column_id,DataTable 的行为就那么奇怪?

【问题讨论】:

    标签: python pandas datatable plotly-dash


    【解决方案1】:

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

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

    列(字典;可选):

    • type(值等于:'any'、'numeric'、'text'、'datetime';可选):列数据的数据类型。

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

    【讨论】:

      猜你喜欢
      • 2014-12-21
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 2012-10-16
      • 2013-09-15
      • 1970-01-01
      相关资源
      最近更新 更多