Hi , I am trying to render a table by its column name.
The table looks like
if ‘column_id’ contains ‘ask’ then give the column ‘tomato’ color.
if ‘column_id’ contains ‘bid’ then give the column ‘blue’ color.
I read the documentation and I knew I can hard-coded each column, like
style_data_conditional=[ { 'if': { 'column_id': 'ask_time' }, 'backgroundColor': 'tomato' } ]
However, I dont want to hard-code for all the columns. Can someone please advise ? Thanks
Hello @TristanSun,
You don’t have to hard-code for all column names, you can create a list of column names that contains ‘ask’ using pandas (let me know if you need help with that).
Once you have your list ready you can loop through every element :
style_data_conditional=[ { 'if': { 'column_id': col_name }, 'backgroundColor': 'tomato' } for col_name in col_list]
1 Like
Thanks @atharvakatre !
yes. it works.
In summary, the final answer should be
ask_cols = [x for x in df.columns.to_list() if 'ask' in x]
bid_cols = [x for x in df.columns.to_list() if 'bid' in x]
style_data_conditional=([ { 'if': { 'column_id': col_name }, 'backgroundColor': 'tomato' } for col_name in ask_cols] +
[{ 'if': { 'column_id': col_name }, 'backgroundColor': 'dodgerblue' } for col_name in bid_cols}])
Thanks
@TristanSun
Wrap them into a list instead of a dict, like these:
style_data_conditional=[
[{"if": {"row_index": col_name}, "backgroundColor": 'tomato'} for col_name in ask_cols],
[{"if": {"row_index": col_name}, "backgroundColor": 'dodgerblue'} for col_name in bid_cols]
]
2 Likes