Pass a python list to HTMLTemplateFormatter template

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.

I’m no JS whiz but maybe try something like this?

1 Like

I got something that works. I didn’t realize the function iterates over each item in the column one at a time. I thought my_col was an array.

def get_html_formatter(my_col):
    template = """        
        <div style="background:<%=
            (function colorfromint(){
                if(names.includes(my_column)){
                    console.log('Success!');
                    console.log(Object.prototype.toString.call(my_column));
                    return('#e5f108');                    
                }                    
                else{
                    console.log('Fail');
                }
            }()) %>; 
            color: black"> 
        <%= value %>        
        </div>
    """.replace('my_column', my_col).replace('names', str(['Cindy', 'Mike']))
    #
    return HTMLTemplateFormatter(template=template)
2 Likes

@beezerlm1 Yes, HTMLTemplateFormatter is a cell formatter, it formats individual cells at at time (it would be much less useful if it could only format columns of cells at at a time). If there is something that could be explained better to make this more clear, the docstring for HTMLTemplateFormatter is here, a Pull Request to contribute your improvements would be welcomed:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.