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

时间:2019-03-27 12:34:42

标签: python python-3.x datatables styling plotly-dash

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

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

我该怎么做?

1 个答案:

答案 0 :(得分:1)

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

您应该这样做:

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