使用Bokeh在Flask中显示图表,我的hovertools可以工作,但日期除外,这是我在所有图表中的x轴。我已经尝试了一百种代码排列,并广泛阅读了文档和相关问题,但是缺少有关如何在自定义html中使用格式化程序的一些关键元素。这个问题似乎可以解决我的确切问题,但是我看不出答案与实际的自定义html有何关系:bokeh hover tool - format date variable in custom html
数据作为熊猫数据帧传递到bokeh,然后在Bokeh中使用ColumnDataSource。索引和x值绝对是日期时间,Bokeh在图表的其他部分(例如x轴标签和刻度)将它们视为日期时间。
这是我当前在Python中使用的hovertools html:
def create_hover_absolute():
"""Generates the HTML for the Bokeh's hover data tool on our graph."""
hover_html = """
<div class="plot-tooltip">
<div>
<span style="font-weight: bold;">$name</span>
</div>
<div>
$y{$,000}
</div>
<div>
Date: @date{%Y-%m}
</div>
</div>
"""
return HoverTool(tooltips=hover_html, formatters={'date': 'datetime'})
我尝试了多种输入日期的方法,包括:
$x
$x with all sorts of formatting such as {%Y%b}
@date
@ date with all sorts of formatting
我最终用TIB将日期表示为长整数(15位数字)或神秘的三位数数字。像这样:
screenshot showing hover with Date: 118TB%
我可以在show(plot)上在本地Bokeh服务器上完成所有这些工作,但在传递到网页时不能在自定义html中进行所有工作。如何在html中格式化日期?如何包含格式化程序?
谢谢
[正如Eugene Pakhomov在评论中指出的,我所要做的就是在格式化程序中将'date'更改为'@date'。像这样:
return HoverTool(tooltips=hover_html, formatters={'@date': 'datetime'})