根据列值对行进行条件样式设置

Conditional styling a row, based on column value

我想根据列的值设置行的样式,我目前有以下代码:

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)

产生以下结果:

但我真正想要的是将行(在本例中为 tr 1 和 8)设置为绿色背景,而不仅仅是单元格。

我该怎么做才能实现这一目标?

要解决您的问题,您只需删除 style_data_conditional 中的 column_id 参数。所以所有的行都会被涂成绿色。

你应该这样做:

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