基于每个单元格的不同范围的 Bokeh DataTable 颜色单元格

Bokeh DataTable color cells based on different range for each cell

我有一个散景数据 table,这种格式显示了 3 种不同 QC 样品(低浓度、中浓度和高浓度)的测量浓度。

Unnamed: 0      run     9Be      45Sc   51V   52Cr  55Mn........
QC Low Mean  11/14/19   1.16    0.845   1.2   2.5   9.6
QC Med Mean  11/14/19   2.37    0.865   2.0   5.6   10.0
QC Hi Mean   11/14/19   15.28   0.894   12.7  32.6  23.9

这些值中的每一个都有一个可接受的范围table。范围取决于样本(低、中、高)和元素。如果它们超出该范围,我想将单元格涂成红色。

例如:

         if QC Low 9Be was < 1.0 or >1.4 the cell would be colored red.
         if QC Med 9Be was <2.2 or >2.7 the cell would be colored red
         if QC Hi  9Be was <14.5 or >16.9 the cell would be red
         if QC Low 51V was <0.9 or >1.4 the cell would be red
         etc

我将所有这些范围作为单独的列存储在列数据源中(例如 Min9Be 和 Max9Be 等)

我知道你可以设置一个模板并使用 htmlformatter 来格式化类似于此的单元格

template="""
            <div style="background:<%= 
                (function colorfromint(){
                    if(some logic ){
                        return("red")}
                    }()) %>; 
                color: black"> 
            <%= value %>
            </div>
            """
formatter =  HTMLTemplateFormatter(template=template)

但据我所知,这仅在比较整列时才有效?或者只对整列应用一个规则?

是否可以逐个单元地进行?有没有办法实现我想要的?

有可能。据我所知,在 Underscore.js 模板中有两种方法可以实现此目的:

from bokeh.io import show
from bokeh.models import DataTable, TableColumn, ColumnDataSource, HTMLTemplateFormatter

ds = ColumnDataSource(dict(x=list(range(10))))
template_it = """\
<div style="background: <%
    if (value % 2) {
        %>red<%
    } else {
        %>green<%
    } %>; color: black;">
    <%- value %>
</div>
"""

template_js = """\
<%
var bc;
if (value < 3) bc = 'red';
else if (value < 6) bc = 'green';
else bc = 'blue';
%>
<div style="background: <%- bc %>; color: black;">
    <%- value %>
</div>
"""
dt = DataTable(columns=[TableColumn(field='x', name='X with inline template',
                                    formatter=HTMLTemplateFormatter(template=template_it)),
                        TableColumn(field='x', name='X with a JS block',
                                    formatter=HTMLTemplateFormatter(template=template_js))],
               source=ds)

show(dt)