0

I want to add colour to a row only if a row's cell's value is present in another datasource. I mean, I have the list A, and the table B, so I want to colour the row X in B if a cell of the row contains a value from A... I don't even know from where to start..

sooaran
  • 183
  • 1
  • 2
  • 13
  • Possible duplicate of [How to color rows and/or cells in a Bokeh DataTable?](https://stackoverflow.com/questions/50996875/how-to-color-rows-and-or-cells-in-a-bokeh-datatable) – ChesuCR Dec 03 '18 at 21:21
  • @ChesuCR nope. Those examples considers the same table. I need to compare the values of the table with the values of a separate list – sooaran Dec 04 '18 at 02:35
  • Just add a column in the datasource of the datatable with the result of the comparison. Then use this value to color de cells – ChesuCR Dec 04 '18 at 10:43

1 Answers1

0

Pretty much you need to do what ChesuCR mentioned in their comment. To take it one step further, see below a small bokeh application.

If you edit values in the first table, a callback will run and check each 'y' value. An additional column is needed to keep track whether the 'y' values are contained in the seperate list/data source. The value of the additional column is then used to color the cell.

from bokeh.layouts import row
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, TableColumn, HTMLTemplateFormatter
from bokeh.io import curdoc

def update_included(attr, old, new):
    list_a = [float(a) for a in source_1.data['a']]
    ys = source_2.data['y']
    y_in = []
    for i, y in enumerate(ys):
        if y in list_a:
            y_in.append(1)
        else:
            y_in.append(0)
    print(ys, y_in, list_a)
    source_2.data['y_in'] = y_in

source_1 = ColumnDataSource(data={'a':[1001,1100]})

columns = [
    TableColumn(field="a", title="Criteria list")
]

data_table1 = DataTable(source=source_1, columns=columns, width=400, editable=True)


dict1 = {'x':[0]*6, 
         'y':[0,10,12,13,200,2001], 
         'y_in':[0]*6}

source_2 = ColumnDataSource(data=dict1)

template="""
<div style="background:<%= 
    (function colorfromint(){
        if(y_in == 1){
            return("blue")}
        else{return("red")}
        }()) %>; 
    color: white"> 
<%= value %></div>
"""

formater =  HTMLTemplateFormatter(template=template)
columns = [
    TableColumn(field="x", title="x"),
    TableColumn(field="y", title="y",formatter=formater)
]

data_table2 = DataTable(source=source_2, columns=columns, width=400)

source_1.on_change('data', update_included)
curdoc().add_root(row(data_table1, data_table2))
Anthonydouc
  • 3,334
  • 1
  • 16
  • 29