Black Lives Matter. Please consider donating to Black Girls Code today.

Style_data_conditional to compare multiple columns

Is there a way to be able to compare two user-entered values (therefore strings) in two different columns of a table?

For example:

style_data_conditional = [{
        'if': {'column_id': 'a',
        'filter_query': '{a} >= {b}'},
        'background_color': 'red'
    },...

in this example I cannot compare these values, since they are strings.

Hi @Twisterbboy yes you can compare multiple columns, see the example below adapted from the dash docs (apologies for the heresy of comparing temperature and humidity for a fast example!)

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from collections import OrderedDict
import pandas as pd

import dash_table

data = OrderedDict(
    [
        ("Date", ["2015-01-01", "2015-10-24", "2016-05-10", "2017-01-10", "2018-05-10", "2018-08-15"]),
        ("Region", ["Montreal", "Toronto", "New York City", "Miami", "San Francisco", "London"]),
        ("Temperature", [1, -20, 3.512, 4, 10423, -441.2]),
        ("Humidity", [10, 20, 30, 40, 50, 60]),
        ("Pressure", [2, 10924, 3912, -10, 3591.2, 15]),
    ]
)

df = pd.DataFrame(data)


app = dash.Dash(__name__)


app.layout = html.Div(
    [
        dash_table.DataTable(
    data=df.to_dict('records'),
    columns=[
        {'name': i, 'id': i} for i in df.columns
    ],
    style_data_conditional=[{
        'if': {'column_id': 'Temperature',
        'filter_query': '{Temperature} < {Humidity}'},
        'backgroundColor': '#3D9970',
        'color': 'white',
    }]
)
    ]
)



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

Does that work for your problem? If not give examples of your data, preferably with a standalone code.

Unfortunately not! Because in my case I need to compare numbers that cannot be compared within strings. Since in python for example “7” < “68” is false, since the string 7 is compared with the string 6.

I don’t understand why you need your column values to be strings? Even if you table is editable, user-entered values are still numbers if the original values were numbers. For example if the example I included if you make the table editable and changes the numbers the styling will still be correct.