简体   繁体   English

如何为散景数据表中的行和/或单元格着色?

[英]How to color rows and/or cells in a Bokeh DataTable?

Starting from Initial table , I need to highlight(color) elements as shown in either one of the table examples Ex. 1, Ex. 2, Ex. 3Initial table开始,我需要突出显示(颜色)元素,如表示例之一所示Ex. 1, Ex. 2, Ex. 3 Ex. 1, Ex. 2, Ex. 3 Ex. 1, Ex. 2, Ex. 3 . Ex. 1, Ex. 2, Ex. 3 .

在此处输入图片说明

Any idea?任何的想法?

In case someone else might bump into the same need, here are some variants that I came up with.如果其他人可能会遇到同样的需求,这里有一些我想出的变体。 (Thanks to Bokeh team for hints!) (感谢散景团队的提示!)

Variant 1: Highlight the cell where column A > column B变体 1:突出显示A 列 > B 列的单元格

Code:代码:

from bokeh.io import output_notebook, show
output_notebook()

from random import randint
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, HTMLTemplateFormatter

output_file("data_table.html")

data = dict(
    cola=[randint(0, 100) for i in range(10)],
    colb=[randint(0, 100) for i in range(10)],
)
source = ColumnDataSource(data)

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

columns = [TableColumn(field="cola", title="CL1", width = 100),
           TableColumn(field='colb', title='CL2', formatter=formatter, width = 100)]
data_table = DataTable(source=source,
                       columns=columns,
                       fit_columns=True,
                       selectable = True,
                       sortable = True,
                       width=400,height=400)

show(widgetbox(data_table))

Output:输出:

在此处输入图片说明

Variant 2: Highlight the cell where column A > column B and color the text变体 2:突出显示A 列 > B 列的单元格并为文本着色

from bokeh.io import output_notebook, show
output_notebook()

from random import randint
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, HTMLTemplateFormatter

output_file("data_table.html")

data = dict(
    cola=[randint(0, 100) for i in range(10)],
    colb=[randint(0, 100) for i in range(10)],
)
source = ColumnDataSource(data)

template="""
            <div style="background:<%= 
                (function colorfromint(){
                    if(cola > colb ){
                        return("green")}
                    }()) %>; 
                color: <%= 
                    (function colorfromint(){
                        if(cola > colb){return('yellow')}
                        }()) %>;"> 
                <%= value %>
                </font>
            </div>
            """
formatter =  HTMLTemplateFormatter(template=template)

columns = [TableColumn(field="cola", title="CL1", width = 100),
           TableColumn(field='colb', title='CL2', formatter=formatter, width = 100)]
data_table = DataTable(source=source,
                       columns=columns,
                       fit_columns=True,
                       selectable = True,
                       sortable = True,
                       width=400,height=400)

show(widgetbox(data_table))

Output:输出:
在此处输入图片说明

Variant 3: Color the text based on multiple conditions between column A and column B变体 3:根据 A 列和 B 列之间的多个条件为文本着色

from bokeh.io import output_notebook, show
output_notebook()

from random import randint
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, HTMLTemplateFormatter

output_file("data_table.html")

data = dict(
    cola=[5, 6, 7, 20, 30, 40, 50, 60, 70, 80],
    colb=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
)
source = ColumnDataSource(data)

template="""                
            <p style="color:<%= 
                (function colorfromint(){
                    if (1 < Math.abs(cola - colb) && Math.abs(cola - colb) < 10)
                        {return('green')}
                    else if (10 < Math.abs(cola - colb) && Math.abs(cola - colb) < 40)
                        {return('blue')}
                    else 
                        {return('red')}
                    }()) %>;"> 
                <%= value %>
            </p>
            """
formatter =  HTMLTemplateFormatter(template=template)

columns = [TableColumn(field="cola", title="CL1", width = 100),
           TableColumn(field='colb', title='CL2', formatter=formatter, width = 100)
          ]
data_table = DataTable(source=source,
                       columns=columns,
                       fit_columns=True,
                       selectable = True,
                       sortable = True,
                       width=400,height=400)

show(widgetbox(data_table))

Output:输出:
在此处输入图片说明

Variant 4: Color the text based on multiple conditions between column A and column B. Add column CL3 to highlight the conditions变体 4:根据 A 列和 B 列之间的多个条件为文本着色。添加列 CL3 以突出显示条件

from bokeh.io import output_notebook, show
output_notebook()

from random import randint
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, HTMLTemplateFormatter

output_file("data_table.html")

data = dict(
    cola=[randint(0, 100) for i in range(10)],
    colb=[randint(0, 100) for i in range(10)],
    colc=['&#9608;' for i in range(10)]
)

source = ColumnDataSource(data)

template="""                
            <p style="color:<%= 
                (function colorfromint(){
                    if (1 < Math.abs(cola - colb) && Math.abs(cola - colb) < 10)
                        {return('green')}
                    else if (10 < Math.abs(cola - colb) && Math.abs(cola - colb) < 40)
                        {return('blue')}
                    else 
                        {return('red')}
                    }()) %>;"> 
                <%= value %>
            </p>
            """
formatter =  HTMLTemplateFormatter(template=template)

columns = [TableColumn(field="cola", title="CL1", width = 100),
           TableColumn(field='colb', title='CL2', formatter=formatter, width = 100),
           TableColumn(field='colc', title='CL3', formatter=formatter, width = 5)
          ]
data_table = DataTable(source=source,
                       columns=columns,
                       fit_columns=True,
                       selectable = True,
                       sortable = True,
                       width=400,height=400)

show(widgetbox(data_table))

Output:输出:
在此处输入图片说明

I don't know how to do example 1 and 3. But you can do example 2.我不知道如何做例子 1 和 3。但你可以做例子 2。

Here it is with custom css in a folder app:这是文件夹应用程序中的自定义css:

myapp/templates/styles.css : myapp/templates/styles.css

.bk-root .slick-cell.selected {
    background: lightblue;
    color:red;
}

And the html template:和 html 模板:

/myapp/templates/index.html : /myapp/templates/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    {{ bokeh_css }}
    {{ bokeh_js }}
  </head>
  <body>
    <style type="text/css">
      {% include 'styles.css' %}
    </style>
    {{ plot_div|indent(8) }}
    {{ plot_script|indent(8) }}
  </body>
</html>

And in myapp/main.py just have your code that makes the table而在myapp/main.py只有制作表格的代码

in case this is useful to anyone else...如果这对其他人有用...

if you just want to make a table with a column which has a background color, picked per row, based on values of the data source...如果您只想制作一个带有背景颜色列的表格,根据数据源的值,每行选取...

data = {'x': range(20), 'color': [Category10[10][i % 10] for i in range(20)]}
data_source = ColumnDataSource(data)
template = """                
            <div style="background:<%= color %>;">
                &ensp;
            </div>
            """
formatter = HTMLTemplateFormatter(template=template)
columns = [
    TableColumn(field='color', title='Key', formatter=formatter),
    TableColumn(field='x', title='X'),
    # ...
]
table = DataTable(source=data_source, columns=columns)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM