【问题标题】:Conditional styling a row, based on column value根据列值对行进行条件样式设置
【发布时间】:2019-03-27 12:34:41
【问题描述】:

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

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

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

【问题讨论】:

  • 如果你不介意加入一些普通的javascript,你可以使用类似于this related question中给出的解决方案。

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


【解决方案1】:

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

你应该这样做:

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

【讨论】:

  • 哇,这很容易......谢谢
猜你喜欢
  • 2021-07-30
  • 2015-10-29
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多