如何调整 Bokeh DataTable 中行的高度和格式

How to adjust height and formatting of rows in Bokeh DataTable

我正在尝试调整散景 DataTable 的标题和单元格的 font-size 以及 pop-up 消息。 使用 HTMLTamplateFormatter 这样做时,我可以获得 font-size 增加,但是,我不知道如何增加行的高度。

我也不知道如何调整列的 top/title 单元格的 formatting/height。

我能够使用 this 问题中提供的信息将标题加粗。有没有类似的代码可以用来增加 font-size.

悬停单元格时我想显示一些信息,我也想格式化它,可以吗?如果是的话怎么办?

这是我目前拥有的最小示例:

import pandas

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


data = pandas.DataFrame({"x": [1, 2, 3, 4],
                     "y": [200, 3, 4, 5]})
source = ColumnDataSource(data=data)

columns = []

# column 1 with bold title: x and 200% font-size
template200 = """
<div title="<%= x %>" style="font-size: 200%">
<%= value %>
</div>
"""
htmltemplateformatter200 = HTMLTemplateFormatter(template=template200)
col = "x"
title = "<b>%s</b>" % col
columns.append(TableColumn(field=col, title=title,
                           width=50, editor=None,
                           formatter=htmltemplateformatter200))

# column 2 with normal title: y and 400% font-size

template400 = """
<div title="<%= x %>" style="font-size: 400%">
<%= value %>
</div>
"""
htmltemplateformatter400 = HTMLTemplateFormatter(template=template400)
col = "y"
columns.append(TableColumn(field=col, title=col,
                           width=50, editor=None,
                           formatter=htmltemplateformatter400))

data_table = DataTable(source=source, columns=columns, row_headers=False,
                       sortable=False)

show(data_table)

此代码将使 table 单元格中的文本变大。但是,当我将大小更改为 400% 时,文本大于行的可视化高度。

谁能帮我解决这个问题?

干杯, 戴夫

第一次编辑:

展示我想做的事情:

就我而言,我认为我只需要一个行高,因为我希望所有 font-size 都相同。

第二次编辑:

经过大阪烧的回复,我又看了一遍我的代码,想出了以下解决方案:

main.py:

import pandas

from bokeh.models import (ColumnDataSource, TableColumn, DataTable)
from bokeh.models.widgets import HTMLTemplateFormatter
from bokeh.io import curdoc


data = pandas.DataFrame({"x": [1, 2, 3, 4],
                        "y": [200, 3, 4, 5]})
source = ColumnDataSource(data=data)

columns = []

# column 1 with bold title: x and 20px font-size
template200 = """
<div title="<%= x %>" style="font-size: 20px;">
<%= value %>
</div>
"""
htmltemplateformatter200 = HTMLTemplateFormatter(template=template200)
col = "x"
title = "<b>%s</b>" % col
columns.append(TableColumn(field=col, title=title,
                           width=50, editor=None,
                           formatter=htmltemplateformatter200))


# column 2 with bold title: y and 20px font-size
template400 = """
<div title="<%= y %>" style="font-size: 20px;">
<%= value %>
</div>
"""
htmltemplateformatter400 = HTMLTemplateFormatter(template=template400)
col = "y"
columns.append(TableColumn(field=col, title=col,
                           width=50, editor=None,
                           formatter=htmltemplateformatter400))

data_table = DataTable(source=source, columns=columns, row_headers=False,
                       sortable=False, height=1000, fit_columns=True)

curdoc().add_root(data_table)

index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $(function() {
    $( document ).tooltip();
  });
  </script>
    <meta charset="utf-8">
    {{ bokeh_css }}
    {{ bokeh_js }}
    <style>
      {% include 'jquery-ui.css' %}
      {% include 'styles.css' %}
    </style>
  </head>

  <body>
    <div class="content">
      {{ plot_div|indent(8) }}
    </div>
    {{ plot_script|indent(8) }}
  </body>
</html>

styles.css:

.ui-tooltip {
padding: 8px;
position: absolute;
z-index: 9999;
max-width: 500px;
-webkit-box-shadow: 0 0 5px #aaa;
box-shadow: 0 0 5px #aaa;
opacity: 1;
font-size: 20px;
}

这么少的东西,事实证明不可能使用 data_table 来设置可变行高。这是底层 Js 库 - Slick Grid 设置的限制。

https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/cg01WWfpdhw 这里有一些与散景相关的讨论。

然而,您可以轻松地在每一行上设置可变样式,这可以在 python 内设置。使用外部 css sheet 可以更轻松地完成 text/styles 更改 - 虽然可以在 HTML 格式化程序中完成,但非常混乱。

创建以下应用程序结构:(到 运行 在命令提示符下键入 bokeh serve --show myapp

myapp
   |
   +---main.py
   |---templates
           +---index.html
           +---styles.css

main.py(包含在 myapp 目录中):

import pandas

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


x0 = [1,"8px", "red", "bold", "hover 1"]
x1 = [2,"10px", "blue", "italic", "hover 2"]
x2 = [3,"12px", "green", "bold", "hover 3"]
x3 = [4,"14px", "orange", "strong", "hover 4"]
x = [x0,x1,x2,x3]
data = pandas.DataFrame({"x": x,
                     "y": [200, 3, 4, 5]})
source = ColumnDataSource(data=data)

columns = []

# now in the formatter value[0] = original x value
# value[1] = desired font size
# value[2] = desired font color
# value[3] = desired font style
# value[4] = text to display on hover
# you could feed in any styles you want, or do it externally via css + js
template200 = """
<div title="<%= x %>" style="font-size: <%= value[1]%> ; 
color: <%=value[2]%>; font-weight:<%=value[3] %>;" >
<div class = "parent_div"> 
<span class="nonhover"> <%= value[0] %> </span>
<span class= "cell_hover"><%= value[4]%></span> </div>
</div>
"""
htmltemplateformatter200 = HTMLTemplateFormatter(template=template200)
col = "x"
title = "<b>%s</b>" % col
columns.append(TableColumn(field=col, title=title,
                           width=50, editor=None,
                           formatter=htmltemplateformatter200))

# column 2 with normal title: y and 400% font-size

template400 = """
<div title="<%= x %>" style="font-size: 20px;">
<%= value %>
</div>
"""
htmltemplateformatter400 = HTMLTemplateFormatter(template=template400)
col = "y"
columns.append(TableColumn(field=col, title=col,
                           width=50, editor=None,
                           formatter=htmltemplateformatter400))

data_table = DataTable(source=source, columns=columns, row_headers=False,
                       sortable=False,height = 1000, fit_columns=True)

curdoc().add_root(data_table)

styles.css(包含在目录模板中) 基本上你在 div 中有两个跨度。当您将鼠标悬停在它们上方时,显示 属性 设置为 none - 因此不可见。之前不可见的另一个跨度然后将其显示设置为内联,变得可见。你可以通过这个改变鼠标悬停在文本上的样式。

另请注意,因为两个跨度都包含在最外层的 div 中,它具有通过 python 设置的样式,悬停文本和默认文本都具有相同的 css 样式属性。

.nonhover{
display: inline;
}

.cell_hover{
display: none;
background: yellow;
}

.parent_div:hover .nonhover{
display: none;
}

.parent_div:hover .cell_hover{
display: inline;
background: yellow;
}

index.html(包含在目录模板中)

<!DOCTYPE html>
<html lang="en">
  <head>
  <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script>
    <meta charset="utf-8">
    {{ bokeh_css }}
    {{ bokeh_js }}
    <style>
      {% include 'styles.css' %}
    </style>
  </head>

  <body>
    <div class="content">
      {{ plot_div|indent(8) }}
    </div>
    {{ plot_script|indent(8) }}
  </body>
</html>

希望仍然有帮助。