Announcing Dash Bio 1.0.0 🎉 : a one-stop-shop for bioinformatics and drug development visualizations.

How to change background color of a cell based on the another cell value? (Conditional style in dash table)

Consider I have the following table. I want to change the background color of the cell in row 1 to green if the cell value in row 3 in column ‘a’ equals 1. How can I do this? (Conditional style in dash table)

tt

HI @hamedk and welcome to the Dash community :slight_smile:

One way to do this is to use a variable to update the background color for the cells.

Here is an example:


import dash
import dash_table
import pandas as pd


data = dict([("a", [4, 2, 1]), ("b", [6, 3, 0])])
df = pd.DataFrame(data)

bg_color = "white"
if df.iloc[2]["a"] == 1:
    bg_color = "green"

app = dash.Dash(__name__)

app.layout = dash_table.DataTable(
    data=df.to_dict("records"),    
    columns=[{"name": i, "id": i} for i in df.columns],    
    style_data_conditional=[
        {
            "if": {"row_index": 0, "column_id": "a"},
            "backgroundColor": bg_color,            
        },
    ],
)

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

Thank you so much :v:

1 Like