Dash DataTable individual highlight using style_data_conditionals works unusual

隐身守侯 提交于 2020-08-08 05:24:28

问题


Morning,

I'm working on a Project using Python3, Flask and Dash. I'm visualizing a CSV Table using the DataTable() from dash_table and want to highlight some specific cells.

Accordistrong textng the documentation of table styling, this can be done by using the style_data_conditional attribute inside of the DataTable definition. [ https://dash.plot.ly/datatable/style ]

My CSV table looks like this:

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

When trying to access the first column, all style changes are working.

[...]
style_data_conditional=[
    {
        'if': {
            'column_id': 'hostname',
            'filter_query': '{hostname} eq "testclient"'
        },
        'color': 'green',
    }
],
[...]

But when trying to access any other row column like "ftp" or "http", it won't work and even if I use the debug=True parameter at the app.run(...) function call, I get no error output.

[...]
style_data_conditional=[
    {
        'if': {
            'column_id': 'ftp',
            'filter_query': '{ftp} eq "True"'
        },
        'color': 'green',
    }
],
[...]

There's an order of "style" attributes inside of the 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

... but as you can see, the given style attribute is the first mentioned in the listing.

The table is defined like this:

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

Do you have any clue, why the DataTable is behaving that strange just by changing the column_id? Hope you can help me, would be great to use Flask and Dash for this project ^^

Best regards!


回答1:


Since you don't provide column headers with your csv example, I can only assume that ftp and http refer to boolean columns?

The support for boolean-type columns in Dash DataTables seems to be anywhere between limited and non-existent. filter_query expressions don't seem to work with boolean values. The documentation doesn't even mention a boolean data type for columns:

columns (dict; optional):

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

I worked around this by setting the data type for all boolean columns to str in my dataframe:

for col, dtype in df.dtypes.items():
    if dtype == 'bool':
        df[col] = df[col].astype('str')

Then, conditional styling works as expected:

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',
        }
    ]
)



来源:https://stackoverflow.com/questions/60125805/dash-datatable-individual-highlight-using-style-data-conditionals-works-unusual

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!