Truthiness in style_data_conditional filter_query expression for dash_table.DataTable

I am trying to highlight rows in a DataTable if a column is False. I can’t seem to get it to work. Here’s what I’ve tried. Several failed attempts are left as comments.

dash_table.DataTable(
        ...
        style_data_conditional=  {
            "if": {
                # "filter_query": "{my_boolean_column}",
                # "filter_query": "{my_boolean_column} = True",
                # "filter_query": "{my_boolean_column} = true",
                "filter_query": "{my_boolean_column} = 'true'",
            },
            "backgroundColor": "#FF4136",
        },
    )

)

I also tried each of those permutations with my Boolean column declared as type text, to no avail.

Hi @tom-kaufman

With the DataTable, I couldn’t figure out how to make this work with Boolean columns but I was able to make it work if the column was text:

from dash import Dash, html, dash_table
import pandas as pd

data = {
    "ticker": ["AAPL", "MSFT", "AMZN", "GOOGL"],
    "company": ["Apple", "Microsoft", "Amazon", "Alphabet"],
    "quantity": [75, 40, 100, 50],
    "sell": ["True", "False", "True", "False"],
}
df = pd.DataFrame(data)

style_data_conditional=[
        {
            'if': {
                'filter_query': '{sell} = True',
            },
            'backgroundColor': "#FF4136",
        },
    ]


table = dash_table.DataTable(
    columns=[{"name": i, "id": i} for i in df.columns],
    data=df.to_dict("records"),
    style_data_conditional=style_data_conditional
)


app = Dash(__name__)

app.layout = html.Div(table, style={"margin": 20})

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

It works with Dash AG Grid though! Here’s an example: :tada:


import dash_ag_grid as dag
from dash import Dash, html
import pandas as pd

data = {
    "ticker": ["AAPL", "MSFT", "AMZN", "GOOGL"],
    "company": ["Apple", "Microsoft", "Amazon", "Alphabet"],
    "quantity": [75, 40, 100, 50],
    "sell": [True, False, True, False],
}
df = pd.DataFrame(data)

rowStyle = {
    "styleConditions": [
        {
            "condition": "params.data.sell",
            "style": {"backgroundColor": "#FF4136"},
        },
    ]
}


grid = dag.AgGrid(
    columnDefs=[{"field": i} for i in df.columns],
    rowData=df.to_dict("records"),
    columnSize="responsiveSizeToFit",
    getRowStyle=rowStyle,
)


app = Dash(__name__)

app.layout = html.Div(grid, style={"margin": 20})

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

For more information about styling rows in the grid see:

Thanks for taking a crack at it. I felt like I must have been missing something crazy obvious. But it seems like this is a shortcoming of the filter_query syntax, no? Simple boolean expressions ought to be doable. Maybe this is worth opening an issue on github?

Or it’s a good time to switch to Dash Ag Grid :slight_smile:

3 Likes

If you still want to use dash_table, you can convert the format of the column you want to use to style_data_conditional by something like df['my_boolean_column'] = df['my_boolean_column'].astype(str).