defining a style_data_conditional that evaluate the values in each columns, I think is a conbination of this link with other examples in the same page:
First bild the conditional formating in a variable (using a loop that goes to each element of the table) and then asigning this variable to the style_data_conditional
Using the 2) option I mentioned, this is the solution you couldn’t find:
import dash
import dash_html_components as html
import dash_table
from dash.dependencies import Output, Input
import pandas as pd
# create DataFrame
data = {'x': [2, 5, 67, 34, 8, 90, 123, 32],
'y': [7, 34, 56, 9, 10, 45, 3, 123],
}
df = pd.DataFrame (data, columns = ['x','y'])
# Set the style conditioning in cond
cond = []
# evaluate each row in the DataFrame
for i in range(len(df)):
# first column is > than second column + 5
if df.iloc[i]["x"] > df.iloc[i]["y"]+5:
# set the if condition for this row
condition= {'if': {
'row_index': i,
'column_id': 'x'}, 'backgroundColor': '#222222', 'color': 'white'}
# add this condition to a final variable to show in the table
cond.append(condition)
print(cond)
app = dash.Dash(__name__)
app.layout = html.Div([
dash_table.DataTable(id="my_table",
data=df.to_dict('records'),
columns=[{'id': "x", 'name': "x"}, {'id': "y", 'name': "y"}],
style_data_conditional=cond
)
])
if __name__ == '__main__':
app.run_server(debug=True)