本文介绍了如何将HoverTool添加到数据表(Bokeh,Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试 bokeh数据表.是否可以在bokeh表的每个字段中添加 HoverTool ?
I am experimenting with bokeh data table. Is it possible to add HoverTool to each field in bokeh table?
DataTable的示例-
An example of DataTable-
以及HoverTool的工作方式示例-
And and example of how HoverTool works-
推荐答案
可以使用HTMLTemplateFormatter
:
main.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)
稍微好一点的方法(虽然会更痛苦)将使用具有某些CSS样式的其他模板.
A slightly nicer way (though a bit more painful) would use a different template with some CSS styling.
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>
这篇关于如何将HoverTool添加到数据表(Bokeh,Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!