Search code examples
pythonpython-3.xdatatablesstylingplotly-dash

Conditional styling a row, based on column value


I would like to style row based on a value of a column, I currently have this code:

import dash
import dash_table
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')

app = dash.Dash(__name__)
print([{"name": i, "id": i} for i in df.columns])
app.layout = dash_table.DataTable(
    id='table',
    columns=[{"name": i, "id": i} for i in df.columns],
    data=df.to_dict("rows"),
    style_data_conditional=[
        {
        'if': {
                'column_id': 'Number of Solar Plants',
                'filter': '"Number of Solar Plants" > num(100)'
            },
            'backgroundColor': '#3D9970',
            'color': 'white',
        }
    ],
)

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

Which produces the following result:

enter image description here

But what I really want is for the rows (tr 1 and 8 in this case) to be styled with a green background, not just the cells.

What can I do to achieve this?


Solution

  • to fix your issue you just have to remove the column_id parameter in your style_data_conditional. So all the row will be colored in green.

    You should do this:

    style_data_conditional=[
            {
            'if': {
                    'filter': '"Number of Solar Plants" > num(100)'
                },
                'backgroundColor': '#3D9970',
                'color': 'white',
            }
    ]