首页 > 解决方案 > 根据列值对行进行条件样式

问题描述

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

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)设置为绿色背景,而不仅仅是单元格。

我能做些什么来实现这一目标?

标签: pythonpython-3.xdatatablesstylingplotly-dash

解决方案


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

你应该做这个:

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

推荐阅读