I have a DataTable using a ColumnDataSource with Pandas data frame as an input. The field names in my column names are dates in the format d-m-y [03-01-2021]. Everything works well until the point where I try to use a HTMLTemplateFormatter to add background colors for my cells.
from bokeh.io import show
from random import randint
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, HTMLTemplateFormatter
output_file("data_table.html")
data = {
'a':['PASS','FAIL','WARN'],
'b':['FAIL','PASS','WARN']
}
source = ColumnDataSource(data)
template = """
<div style="background:<%=
(function colorfromint(){
if(result_col == 'PASS'){
return('#f14e08')}
else if (result_col == 'FAIL')
{return('#8a9f42')}
else if (result_col == 'WARN')
{return('#8f6b31')}
}()) %>;
color: white">
<%= value %>
</div>
""".replace('result_col',"a")
formatter = HTMLTemplateFormatter(template=template)
columns = [TableColumn(field='a', title="Feb", formatter=formatter, width = 100),
TableColumn(field='b', title='March', formatter=formatter, width = 100)]
formatter=formatter,
data_table = DataTable(source=source,
columns=columns,
fit_columns=True,
selectable = True,
sortable = True,
width=400,height=400)
show(data_table)
However, in the above code the field names are letters such as ‘a’ and ‘b’. If I change those to a date ‘01-01-2021’, the above template formatting does not work. How do I refer to non string values in htmltemplate formatter?
Using strings as fields:
If I use a date as a field:
sample code to reproduce:
from bokeh.io import show
from random import randint
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, HTMLTemplateFormatter
output_file("data_table.html")
data = {
'01-01-2021':['PASS','FAIL','WARN'],
'b':['FAIL','PASS','WARN']
}
source = ColumnDataSource(data)
template = """
<div style="background:<%=
(function colorfromint(){
if(result_col == 'PASS'){
return('#f14e08')}
else if (result_col == 'FAIL')
{return('#8a9f42')}
else if (result_col == 'WARN')
{return('#8f6b31')}
}()) %>;
color: white">
<%= value %>
</div>
""".replace('result_col',"01-01-2021")
formatter = HTMLTemplateFormatter(template=template)
columns = [TableColumn(field='01-01-2021', title="Feb", formatter=formatter, width = 100),
TableColumn(field='b', title='March', formatter=formatter, width = 100)]
formatter=formatter,
data_table = DataTable(source=source,
columns=columns,
fit_columns=True,
selectable = True,
sortable = True,
width=400,height=400)
show(data_table)