DataTable - Negative Numbers

I created a bokeh DataTable from a pandas DataFrame using the following code:

source = ColumnDataSource(df)
columns = [TableColumn(field=col, title=col) for col in df]
data_table = DataTable(source=source, columns=columns, reorderable=False, index_width=0, sizing_mode='scale_width', height=800)  

The DataTable contains multiple columns. Some of the columns contain data in a “string” format.
Other columns contain integers. I would like to have the columns that contain integers display positive amounts in black font and negative amounts in red font. How can I make this happen without needing to specify each column in my code above?

Overall, I am trying to obtain a similar look to Excel’s conditional formatting.

Thanks for your help!

Hi Jacob!

It sounds like there are two questions here, and please correct me if I’ve misunderstood-- how to assign colors conditionally based on numeric value, and how to automatically distinguish between char and numeric values.

As far as applying color conditionally to a column’s values, I think this SO post provides a great starting point-- the second code block in the answer post shows a way to set color (and number format, if you wanted) based on value. They’re setting the div color, so you’d just set the font color. (They also explicitly set the color in their data dict, but you can use a mapping function to do that.)

The limitations here with respect to your question are that they’re specifically assigning the formatter for y to the TableColumn for y. Is your concern that you have a non-static set of columns?

Thank you for your response. My main concern is that I do not have a static set of columns within my pandas DataFrame. So, I was hoping to avoid needing to specifically define each TableColumn and assigning the formatter to each column. However, it does not sound like that is possible based on your response.

Well, I wouldn’t give up yet-- I wonder if it’s possible within the python (or the js template) to do a check on whether the values in a column are numeric, and then conditionally apply the formatter that way. Something to try.