根据列值有条件地设置样式

问题描述 投票:0回答:1

我想基于列的值来设置行的样式,我目前有这样的代码:

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)

这产生以下结果:

enter image description here

但我真正想要的是用绿色背景设置行(在这种情况下为tr 1和8),而不仅仅是单元格。

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

python python-3.x datatables styling plotly-dash
1个回答
1
投票

要解决您的问题,您只需要删除column_id中的style_data_conditional参数。因此所有行都将以绿色着色。

你应该做这个:

style_data_conditional=[
        {
        'if': {
                'filter': '"Number of Solar Plants" > num(100)'
            },
            'backgroundColor': '#3D9970',
            'color': 'white',
        }
]
© www.soinside.com 2019 - 2024. All rights reserved.