Conditional Formating With Table

Hi,

I have a dash_table.Table() in my dashboard. And I wanna some conditional format.

But I have a condition like that:

if (x > y+5):

How can I do this?
x and y are different columns. You can imagine like this: X: Weight, Y: ideal Weight.

I wanna change the color of the box with this condition. How can I do this???

I think there are two different ways.

  1. 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:

https://dash.plotly.com/datatable/conditional-formatting

  1. 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

Thank you. But I know how to use style data condition, but I wanna use it with different thing.

{
‘if’: {
‘filter_query’: ‘{X} > {Y}’,
“column_id”: “X”
},
‘backgroundColor’: #222222,
‘color’: ‘white’
},

This is my current condition. But I wanna compare X column with Y column values +5.
So I wanna use smth like this:

{
‘if’: {
‘filter_query’: ‘{X} > {Y+5}’,
“column_id”: “X”
},
‘backgroundColor’: #222222,
‘color’: ‘white’
},

But this is not possible. How can I do this??

Hi @bilallozdemir

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)