Updating HTMLTemplateFormatter with a button click

I have a working example of a table where the transaction column is formatted based on a list of names. I would like to update the formatting through an action such as clicking a button. Is this possible to do on a bokeh server app after the page is loaded?

In this example, I would like to change the formatting from the rows containing the names Mike and Cindy to the rows containing the names Nick and Jim.

import pandas as pd
from bokeh.io import curdoc
from bokeh.layouts import layout, column
from bokeh.models import (ColumnDataSource, DataTable, Button,
                          TableColumn, HTMLTemplateFormatter)

def get_html_formatter(my_col):
    template = """        
        <div style="background:<%=
            (function colorfromint(){
            }()) %>; 
            color: black"> 
        <%= value %>        
    """.replace('my_column', my_col).replace('names', str(name_formatted))
    return HTMLTemplateFormatter(template=template)

def update():
    current = df
    source.data = {
        'transaction'   : current.transaction,
        'date'          : current.date,        
        'name'          : current.name

def callback_update_formatting():
    name_formatted = ['Nick', 'Jim']
    # Do something here that removes formatting from rows with names Cindy & Mike,
    # Then adds formatting to rows with names Nick & Jim
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']
name_formatted = ['Cindy', 'Mike']

my_dict = {'transaction':transaction, 'date':date, 'name':name}

df = pd.DataFrame(my_dict)

button_update_formatting = Button(label='Update Formatting', button_type="success", css_classes=["custom_bk_btn"])

source = ColumnDataSource(data=dict())

columns = [
    TableColumn(field="transaction", title="Transaction_ID", formatter=get_html_formatter('name')),
    TableColumn(field="date", title="Date"),
    TableColumn(field="name", title="Name")

data_table = DataTable(source=source, columns=columns, fit_columns=True, width=400, editable=True)

curdoc().add_root(column(button_update_formatting, data_table))
curdoc().title = "Example"


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