简体   繁体   English

Plotly 破折号:单个单元格的 data_table 背景颜色

[英]Plotly Dash: data_table background color for individual cell

I display in a dash_table.DataTable a dataframe where there is a column with color names in hex format, with this code:我在dash_table.DataTable中显示 dataframe ,其中有一列颜色名称为十六进制格式,代码如下:

import dash
import dash_table
import pandas as pd

df = pd.DataFrame(data = dict(COLOR = ['#1f77b4', '#d62728', '#e377c2', '#17becf', '#bcbd22'],
                              VALUE = [1, 2, 3, 4, 5]))

app = dash.Dash(__name__)

app.layout = html.Div([dash_table.DataTable(id = 'table',
                                            columns = [{"name": i, "id": i} for i in df.columns],
                                            data = df.to_dict('records'))],
                      style = dict(width = '200px'))

if __name__ == '__main__':
    app.run_server()

This is what I get:这就是我得到的:

在此处输入图像描述

I would like to set the background color (and maybe the font color) of each individual cell with its content, but only for that column (which is always the first column of the table) in order to get this:我想设置每个单独单元格的背景颜色(可能还有字体颜色)及其内容,但仅限于该列(始终是表格的第一列)以获得此:

在此处输入图像描述

To me is ok to replace the dash_table.DataTable with plotly.graph_objects.Table ( documentation ), which maybe it is more customizable;对我来说可以将dash_table.DataTable替换为plotly.graph_objects.Table文档),这可能更可定制; provided that I can implement the plotly.graph_objects.Table in a dash dashboard.前提是我可以在dash板中实现plotly.graph_objects.Table

Version info:版本信息:

Python               3.7.0
dash                 1.12.0
dash-table           4.7.0
plotly               4.7.0

You can define the background color and the font color (as well as several other properties) of each individual cell using style_data_conditional , see https://dash.plotly.com/datatable/style .您可以使用style_data_conditional每个单独单元格的背景颜色和字体颜色(以及其他几个属性),请参阅https://dash.plotly.com/datatable/style

import dash
import dash_table
import dash_html_components as html
import pandas as pd

df = pd.DataFrame(data=dict(COLOR=['#1f77b4', '#d62728', '#e377c2', '#17becf', '#bcbd22'],
                            VALUE=[1, 2, 3, 4, 5]))

app = dash.Dash(__name__)

app.layout = html.Div([

    dash_table.DataTable(
        id='table',
        columns=[{'name': i, 'id': i} for i in df.columns],
        data=df.to_dict('records'),
        style_data_conditional=[{'if': {'row_index': i, 'column_id': 'COLOR'}, 'background-color': df['COLOR'][i], 'color': df['COLOR'][i]} for i in range(df.shape[0])]
    ),

], style=dict(width='100px'))

if __name__ == '__main__':
    app.run_server()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM