您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

具有条件颜色的单元格的bokeh数据表

具有条件颜色的单元格的bokeh数据表

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

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

根据您使用的bokeh版本,您可能需要在HTML文档中包括lodash或下划线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)

如果您完全依赖javascript(即没有基于python的回调),则在基础值更改时,此方法不会更新填充颜色。

其他 2022/1/1 18:30:01 有372人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶