散景数据表颜色单元格基于每个单元格的不同范围

时间:2020-06-24 17:53:42

标签: python python-3.x datatable bokeh styling

我有一个bokeh数据表,这种格式显示了3种不同质控样品(低浓度,中浓度和高浓度)的浓度。

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

每个值都有一个可以接受的范围。范围取决于样品(低,中,高)和元素。如果它们不在该范围内,则我想将单元格涂成红色。

例如:

         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

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

我知道您可以设置模板并使用htmlformatter格式化与此类似的单元格

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

但是从我所看到的来看,这仅在比较整个列时有效吗?还是仅对整个列应用一个规则?

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

1 个答案:

答案 0 :(得分:1)

有可能。 AFAIK在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)