Conditional Formatting in Dash Table 3.7.0?

I was just in the process of moving one of the apps from Dash 0.42 to 0.43 and it looks like I also grabbed Dash Table 3.7.0 ( which I don’t think has a release announcement yet :partying_face:)

It looks like conditioning formatting has changed (judging by how my app has broken) but the documentation isn’t updated yet. Say I have a conditional formatting that looks like this where “Status” and “At_Check” are fields:

style_data_conditional=[
    {'if': {
        'filter': 'Status eq "OK" and At_Check eq "Up"'
        },
        'backgroundColor': '#3D9970',
        'color': 'white'},
]

What would the new syntax be?

And to answer my own question as my afternoon coffee kicked in and I was finally able to parse the release notes on github: https://github.com/plotly/dash-table/blob/master/CHANGELOG.md#370---2019-05-15

style_data_conditional=[
    {'if': {
        'filter': '{Status} eq "OK" and {At_Check} eq "Up"'
        },
        'backgroundColor': '#3D9970',
        'color': 'white'},
]

This is a much nicer syntax than before. But I’m not totally sold on using { and } for fields as I see mixing them with f-strings, e.g.

my_field_1 = 'Status'
my_field_2 = 'At_Check'
style_data_conditional=[
    {'if': {
        'filter': f'{{{my_field_1}}} eq "OK" and {{{my_field_2}}} eq "Up"'
        },
        'backgroundColor': '#3D9970',
        'color': 'white'},
]

But hey you’re really going to notice where you use those!

Hi Damian,

I just came across the same issue and my previously working code throws an error:

[{
  'if': {'column_id': str(x), 'filter': '{} < num(0.9)'.format(x)},
  'background-color': '#9cd5ac'
} for x in ['column1','column2']

]

when I change num(0.9) to just 0.9, the error is gone but my code is still not formatted.
Do you know how to fix this by any chance?

With f-strings you can try something like 'filter': f'{{{x}}} < 0.9'. Note the extra “{”

Hi Rafael,

Thanks for the suggestion, I did not know about Python f-strings previously.
I tried to use them but I get a key error, e.g. when I change my code to

[{
  'if': {'column_id': str(x), 'filter': f'{{{x}}} < 0.9'.format(x)},
  'background-color': '#9cd5ac'
} for x in ['column1','column2']
]

I get "KeyError: ‘column1’

You can’t use both the .format() method and f-strings together, pick one or the other. I.e. do:

f'{{{x}}} < 0.9'

Or:

'{{{}}} < 0.9'.format(x)

It’s up to you but f-strings are the current Pythonic way to implement string substitution as they are both more compact and easier to read. But they only work in Python 3.6+.

It worked, thanks Damian!

where do we define my_field_1 & my_field_2?

I have a very similar problem -

        style_data_conditional =
      # Colour red when values are less than benchmark
      [
        {
          'if': {
                 'column_id': str(column), 
                 
             #'filter_query': f'{{{column}}} < {{{benchmark}}}'},
            'filter_query': f'{{{Fruits}}} eq "Apples" && {{{column}}} < 
                            {{{benchmark}}}'},
 },
          'backgroundColor': 'red',
                    'color': 'white'
        } for column in data.iloc[:,0:].columns

benchmark stores a dropdown selection which will be a column name. The first column of the dataset is Fruits and it won’t be in the list of options in the dropdown. I am having a problem with this f-string.