Problem trying to highlight cells in dataTable

Good morning.

I’m trying to use conditional styles depending on the value of each cell in my DataTable.

An example of my data is the following:

data = pd.DataFrame(data = {‘fc’: [‘row_1’, ‘row_2’, ‘row_3’], ‘param_1’: [1,0,2], ‘param_2’: [3,0,1]})

and my code to show the DataTable:

tabla = DataTable(data = data.to_dict(‘rows’),
columns = [{‘id’: c, ‘name’: c} for c in data.columns],
style_as_list_view = True,
style_cell = {‘fontSize’: 15},
style_cell_conditional = {‘if’: {‘column_id’: ‘param_2’,
‘filter_query’: ‘{param_2} eq 3’},
‘backgroundColor’: ‘rgb(255,0,0)’
},
)

So I want that cells in column ‘param_2’ that have a value equal to 3 have red background, but instead, I’m getting my whole DataTable red.

Thanks you very much in advance!

So I think you are almost there. Here is an example of what I do. The for loop repeats the query for the column_id’s listed.

conditional += [
	{
	    'if': {
	        'column_id': x, 
                'filter': '{' + x + '}' + ' <= ' + str(threshold_max)
             },
	'backgroundColor': highlight
	} for x in ['b', 'd', 'f', 'h', 'j', 'l', 'n']
]

So, your filter_query line should become filter: '{' + param_2 + '}' == str(3)

There could be better ways of how I implemented my query, but it works and should get you going. Also, eq could be valid but since I’m running a version behind 1.0.0, I’m not 100% sure.

You’re super close! Just two things:

  • filter_query only applies to style_data_conditional, not style_cell_conditional (which also covers the headers)
  • It’s an array of objects, not just one object
    DataTable(
        data=data.to_dict('rows'),
        columns=[{'id': c, 'name': c} for c in data.columns],
        style_as_list_view=True,
        style_cell={'fontSize': 15},
        style_data_conditional=[{
            'if': {
                'column_id': 'param_2',
                'filter_query': '{param_2} eq 3'
            },
            'backgroundColor': 'rgb(255,0,0)'
        }],
    )

18%20PM

1 Like

Thanks to you guys!

I think that Alex has right, however, I still have the problem due to unicode issues :(, python 2 facts.