Highlighting Date cells which are older than a week

Hi folks,
I’m trying to colour the dates which are longer than a week ago.
I’ve made a DateTime so I have the date of last week.

week_ago = datetime.today() - timedelta(days=7)

And the date that needs to be checked did I convert to a DateTime

df['Last Successful Build'] = pd.to_datetime(df['Last Successful Build'], format="%d/%m/%Y %H:%M:%S")

The code I use for filtering is this but it colours all the dates.

style_data_conditional=[
            {
                'if': {
                    'column_id' : 'Last Successful Build',
                    'filter_query': '{Last Successful Build} < week_ago'
                },
                'backgroundColor': 'white',
                'color': '#ed0909',
            },                             
        ],

Thanks for the help in advance

See my post here (Conditional formating numerical column) specifically about filter_query vs filter and style_data_conditional vs style_cell_conditional. The version of Dash you are using is also important to check.

Hi, flyingcujo,

I’ve checked my version, and I just changed my version to 1.0.2.
I’ve changed filter_query to filter but I got this error:

Failed component prop type: Invalid component prop style_data_conditional[1].if key filter supplied to DataTable.
Bad object: {
“column_id”: “Last Successful Build”,
“filter”: “{Last Successful Build} < week_ago”
}
Valid keys: [
“column_id”,
“column_type”,
“filter_query”,
“row_index”,
“column_editable”
]

I think I’m doing something stupid here and I’m overseeing the problem also, I don’t think I have to use style_cell_condition because I don’t need to filter the header and the filter cell.

What I’m I doing wrong?

Thanks in advance

I’ve managed to fix the problem by making the data in [‘Last Successful Build’] like this >>> 2019-07-11
Then for the week_ago, I did this:

week_ago = datetime.today() - timedelta(days=7)
week_agoDate = week_ago.strftime("%Y-%m-%d")

And as of last in the dash code where u make your data table I changed it to this:

style_data_conditional=[
            {
                'if': {
                    'column_id' : 'Last Successful Build',
                    'filter_query': ('{Last Successful Build} <' + week_agoDate)
                },
                'backgroundColor': 'white',
                'color': '#ed0909',
            },                             
        ], 
1 Like