问题
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() ...
- style_data_conditional
- style_data
- style_filter_conditional
- style_filter
- style_header_conditional
- style_header
- style_cell_conditional
- 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