gpt4 book ai didi

python - 如何将 HoverTool 添加到数据表(Bokeh、Python)

转载 作者:太空狗 更新时间:2023-10-29 20:51:32 27 4
gpt4 key购买 nike

我正在试验 bokeh data table .是否可以添加 HoverTool Bokeh 表中的每个字段?

DataTable的一个例子- enter image description here

以及 HoverTool 如何工作的示例- enter image description here

最佳答案

这可以使用 HTMLTemplateFormatter:

ma​​in.py:

from os.path import dirname, join
import pandas as pd
from bokeh.io import curdoc, show
from bokeh.models import ColumnDataSource, Div
from bokeh.models.widgets import DataTable, TableColumn, HTMLTemplateFormatter
from bokeh.layouts import layout

template = """<span href="#" data-toggle="tooltip" title="<%= value %>"><%= value %></span>"""

df = pd.DataFrame([
['this is a longer text that needs a tooltip, because otherwise we do not see the whole text', 'this is a short text'],
['this is another loooooooooooooooong text that needs a tooltip', 'not much here'],
], columns=['a', 'b'])

columns = [TableColumn(field=c, title=c, width=20, formatter=HTMLTemplateFormatter(template=template)) for c in ['a', 'b']]

table = DataTable(source=ColumnDataSource(df), columns=columns)

l = layout([[table]])

curdoc().add_root(l)

show(l)

enter image description here

稍微好一点的方法(虽然更痛苦一点)是使用带有一些 CSS 样式的不同模板。

template = """<div class="tooltip-parent"><div class="tooltipped"><%= value %></div><div class="tooltip-text"><%= value %></div></div>"""

desc.html:

<style>
.tooltip-parent {
width: 100%;
}

.tooltipped {
overflow: hidden;
width: 100%;
}

.tooltip-text {
visibility: hidden;
width: 250px;
background-color: rgba(0, 0, 0, 1);
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 5px;
position: relative;
z-index: 1;
top: 100%;
left: 0%;
white-space: initial;
text-align: left;
}

.tooltipped:hover + .tooltip-text {
visibility: visible;
}

div.bk-slick-cell {
overflow: visible !important;
z-index: auto !important;
}
</style>

<h1>Tooltip demo</h1>

enter image description here

关于python - 如何将 HoverTool 添加到数据表(Bokeh、Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34169264/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com