Conditional_formatting multiple DataTables

How can i aplly the conditial formatting to all the DataTables ?

from dash import Dash, dash_table
import pandas as pd
from collections import OrderedDict
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash import html

app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
data = OrderedDict(
    [
        ("Title", ["one", "two", "treem"]),
        ("COL1", [50, 20, 80]),
        ("COL2", [10, 40, 90])
    ]
)

df = pd.DataFrame(data)

########♦
style_cell_conditional = [ {'if': {'column_id': 'Title'},'backgroundColor': 'gray'}]
#########

app.layout = dbc.Container([
    #rij1
    dbc.Row([
        dbc.Col([dash_table.DataTable(data=df.to_dict('records'))],width=3),
        dbc.Col([dash_table.DataTable(data=df.to_dict('records'))],width=3),
        dbc.Col([dash_table.DataTable(data=df.to_dict('records'))],width=3),
        dbc.Col([dash_table.DataTable(data=df.to_dict('records'))],width=3)
        ]),
    #rij2
    dbc.Row([
        dbc.Col([dash_table.DataTable(data=df.to_dict('records'))],width=3),
        dbc.Col([dash_table.DataTable(data=df.to_dict('records'))],width=3),
        dbc.Col([dash_table.DataTable(data=df.to_dict('records'))],width=3),
        dbc.Col([dash_table.DataTable(data=df.to_dict('records'))],width=3)
        ]),  
    ])

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

Hello @marvy,

If these are static, store the formatting as a variable and then load it into each table.

Ok , that’s working.

var = [ {'if': {'column_id': 'Title'},'backgroundColor': 'gray'}]
dbc.Col([dash_table.DataTable(data=df.to_dict('records'),style_cell_conditional = var )],width=3),

Is theren o way to use a class ?

To use a class to apply the conditional formatting?

No, however, you could use pattern-matching callbacks and set your id to be something like:

{‘index’:1, ‘type’:’myFormattedTable’}

Then use a callback:

@app.callback(Output({‘index’:MATCH,’type’:’myFormattedTable’},’style_cell_conditional’),Input({‘index’:MATCH,’type’:’myFormattedTable’},’id’))
def formatTables(t):
    return var 

This will apply conditional formatting to those tables throughout the app upon being displayed. :grin: