带有带条件颜色的单元格的Bokeh数据表 [英] bokeh DataTable with conditionally colored cells

查看:10
本文介绍了带有带条件颜色的单元格的Bokeh数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在bokeh中显示一个数据表,其中的单元格是红色或橙色的,具体取决于单元格的文本内容。

例如,如果单元格包含单词&Quot;Error&Quot;,则该单元格以红色背景显示。 如果单元格包含单词&警告&,则为橙色。

我认为我应该使用[HTMLTemplateFormatter][1],但如何使用?

我该如何做?

谢谢

推荐答案

浏览文档,您可以使用HTMLTemplateForMatter和下划线js来格式化表格。请参见http://docs.bokeh.org/en/latest/docs/reference/models/widgets.tables.htmlhttp://underscorejs.org/#template了解更多信息。 我附上了一个基于整数值进行格式化的示例,尽管您可以根据需要扩展它。

注意:这实质上是在每个表格单元格中嵌入一个div,因此每个表格单元格周围仍有一个小的白色边框。如果可能,您可以调整内部div的大小,或设置父元素的样式。

更新:根据您使用的bokeh版本,您可能需要在您的html文档中包含连字符或下划线js,即:<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script>

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

dict1 = {'x':[0]*6,'y':[0,1,0,1,0,1]}
source = ColumnDataSource(data=dict1)

template="""
<div style="background:<%= 
    (function colorfromint(){
        if(value == 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_table = DataTable(source=source, columns=columns, width=800)

如果您愿意在Python中定义颜色,则更简单的方法是将颜色定义为列数据源中的一个字段,并在模板代码中引用该值。

dict1 = {'x':[0]*6, 
         'y':[0, 1, 0, 1, 0, 1],
         'color':['blue', 'red', 'blue', 'red', 'blue', 'red']}
source = ColumnDataSource(data=dict1)

template="""
<div style="background:<%=color%>"; color="white";>
<%= value %></div>
"""

formater =  HTMLTemplateFormatter(template=template)

如果您完全依赖于Java脚本(即没有基于Python的回调),则如果基础值发生更改,此方法不会更新填充颜色。

这篇关于带有带条件颜色的单元格的Bokeh数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆