Datatable style_data_conditional for empty strings

Hi all,

I have a data table for which i would like blank entries to be highlighted in red. I’m trying to use the style_data_conditional combined with the “filter_query”. I have an example of this working where in the column “Process” and cells with “Process 1” are highlighted, and in “Time/s” any columns with the number 5 are highlighted:

style_data_conditional=[
{
“if”: {
‘column_id’:‘Process’,
“filter_query”: ‘{Process} eq “Process 1”’
},
“backgroundColor”: “red”,
},
{
# “if”: {
# ‘column_id’:‘Task’,
# “filter_query”: ‘{Task} eq 5’
# },
# “backgroundColor”: “red”,
},
{
“if”: {
‘column_id’:‘Time/s’,
“filter_query”: ‘{Time/s} eq “5”’
},
“backgroundColor”: “red”,
},
],

However I can’t get this to work for the blank cells. When I try to filter_query “” it returns the error message:

"unable to evaluate target: syntax tree is invalid for query={Task} eq “” "

style_data_conditional=[
{
“if”: {
‘column_id’:‘Process’,
“filter_query”: ‘{Process} eq “Process 1”’
},
“backgroundColor”: “red”,
},
{
“if”: {
‘column_id’:‘Task’,
“filter_query”: ‘{Task} eq “”’
},
“backgroundColor”: “red”,
},
{
“if”: {
‘column_id’:‘Time/s’,
“filter_query”: ‘{Time/s} eq “5”’
},
“backgroundColor”: “red”,
},
],

Any help for highlighting the blank cells would be greatly appreciated!

Thanks,

Harry

You just stumbled across another bug where it’s currently impossible to create a filter on an empty string.

Here’s a bogus example that works (with the fix)

Issue has been opened here: https://github.com/plotly/dash-table/issues/569

import dash

from dash_table import DataTable

app = dash.Dash(__name__)

app.layout = DataTable(
    id='table',
    columns=[{
        'name': x,
        'id': x,
        'selectable': True
    } for x in ['a', 'b', 'c']],
    data=[{
        'a': str(x) if x % 2 == 0 else '',
        'b': x if x % 3 == 1 else '',
        'c': str(x*x) if x % 4 == 2 else ''
    } for x in range(0,100)],
    style_data_conditional=[{
        'if': {
            'column_id': x,
            'filter_query': '{{{}}} eq ""'.format(x)
        },
        'backgroundColor': 'pink'
    } for x in ['a', 'b', 'c']]
)

if __name__ == '__main__':
    app.run_server(debug=True)

Just confirming this has been fixed and will be in the next release (dash==1.3.0, dash-table==4.3.0)

1 Like

Awesome! thank you very much!

three reasons why you should buy plotly pro: support open source, get great support, host your plots and dashboards online