Conditionally Colour Cells In a hv.Table

Hi everyone!

First post on here, but i have a question. Does any know how to conditionally colour cells within a hv.Table. Code below:

# import libraries

import numpy as np
import pandas as pd
import holoviews as hv
hv.extension('bokeh')

# create sample data
status = np.random.choice(['Green', 'Yellow', 'Red'], size=100)
brand = np.random.choice(['B1', 'B2', 'B3'], size=100)
df = pd.DataFrame({'brand': brand, 'status': status})

create same grouped data as in the question

grouped = df.groupby(['brand', 'status'])['status'].count().unstack().reset_index()

create holoviews table from the grouped data

hv.Table(grouped)

With this data I would like to conditionally colour these cells, where for example, the cell would appear green if the data is => 5.

I’ve created the following function in pandas:

#Heatmap of table
def change_colours(val):
color = 'green' if val => 5 else 'red'
return 'color: %s' % color

When I apply this function to a pandas dataframe the colours change as i am hoping. However, when I attempt to turn this correctly coloured tabled into a hv.Table I am running into trouble:

  • I have tried unstack/reset_index before the dataframe becomes a hv.Table and I get the error '‘Styler’ object has no attribute ‘reset_index’

  • Given its format it doesn’t covert to a hv.Table without unstacking first

I was wondering if anyone has figured out a way to add conditional formatting of cells or the numbers themselves within a hv.Table (I would prefer cells to be coloured rather than the numbers). Would bgcolour accept a function as a parameter? I also tried this but to no success. Grateful for any insight!

Kind regards

Afaik bokeh does not allow coloring cells, this might make a good feature request on Bokeh though.

Thanks for clarifying Phillip! I’ll post it on the bokeh github!

Kind regards,