I am trying to format a column based on the contents of another column. Below is a working example I have that will format a single cell’s color in the ‘transactions’ column based on the contents of the ‘name’ column. I can only figure out how to pass one name though. I would like to place a list of names such as [‘Cindy’, ‘Mike’] in place of the name ‘Cindy’. I have tried everything I can think of and I can’t seem to make it work.
import pandas as pd
from bokeh.io import curdoc
from bokeh.models import (ColumnDataSource, DataTable,
TableColumn, DateFormatter,
HTMLTemplateFormatter )
def get_html_formatter(my_col):
template = """
<div style="background:<%=
(function colorfromint(){
if(name.includes('names')){
return('#e5f108')}
}()) %>;
color: black">
<%= value %>
</div>
""".replace('names', 'Cindy')
return HTMLTemplateFormatter(template=template)
def update():
current = df
source.data = {
'transaction' : current.transaction,
'date' : current.date,
'name' : current.name
}
date = ['12-01 2021', '12-02 2021', '12-03 2021', '12-04 2021', '12-05 2021' ]
transaction = [10,20,30,40,50]
name = ['Mike', 'Jeff', 'Cindy', 'Nick', 'Jim']
my_dict = {'transaction':transaction, 'date':date, 'name':name}
df = pd.DataFrame(my_dict)
source = ColumnDataSource(data=dict())
date_format = DateFormatter(format="%m/%d/%Y")
columns = [
TableColumn(field="transaction", title="Transaction_ID", formatter=get_html_formatter('name')),
TableColumn(field="date", title="Date", formatter=date_format),
TableColumn(field="name", title="Name")
]
data_table = DataTable(source=source, columns=columns, fit_columns=True, width=400)
curdoc().add_root(data_table)
curdoc().title = "Example"
update()
Any help is much appreciated.