Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

python 3.x - Plotly Dash: Conditionally changing datatable cell color

I've got the following code:

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

df = pd.DataFrame(
    columns = ['col1', 'col2'],
    data=[
        ['aaaa', 5],
        ['aa', 6],
        ['bbbb', 7],
        ['cccc',8]

    ]

)
app = dash.Dash(__name__)

styles = [{'if': {
                        'column_id': 'col1',
                        'filter_query': '{col1} contains {val}'
                    },
                    'backgroundColor': background_color,
                    'color': 'white'
                } for val, background_color in zip(['a', 'b', 'c'], 
                            ['#FF0000', "#00FF00", '#0000FF'])]

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_cell={'font-family':'sans-serif'},
        style_data_conditional=styles
)])


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

I'm trying to change the background color of the col1 column if it contains a, b or c. I know that I need to use style_data_conditional, but can't figure out how to reference the column name correctly. Does anyone know how?

question from:https://stackoverflow.com/questions/65623625/plotly-dash-conditionally-changing-datatable-cell-color

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem with the filter query in your code is that val is a string and is therefore not being updated based on the values in the list (i.e. it is always equal to 'val' instead of taking the values 'a', 'b' and 'c'). If you replace val with a variable as in the example below your code should work as expected.

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

df = pd.DataFrame(
    columns=['col1', 'col2'],
    data=[['aaaa', 5],
          ['aa',   6],
          ['bbbb', 7],
          ['cccc', 8]]
)

app = dash.Dash(__name__)

styles = [{'if': {'column_id': 'col1', 'filter_query': '{col1} contains ' + val},
'backgroundColor': background_color, 'color': 'white'} for val, background_color
in zip(['a', 'b', 'c'], ['#FF0000', "#00FF00", '#0000FF'])]

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_cell={'font-family': 'sans-serif'},
        style_data_conditional=styles
)])

if __name__ == '__main__':
    app.run_server(debug=True, host='127.0.0.1')

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...