futureSPQR futureSPQR - 5 months ago 50
Python Question

bokeh hover tool - format date variable in custom html

I have

dt_source = ColumnDataSource(
"date": result_detail['report_date_as_yyyy_mm_dd']
,"contract": result_detail['contract_name']
,"open_interest": result_detail['open_interest']


oi = figure(plot_width=800, plot_height=200, x_axis_type="datetime", tools=tools, title="Open Interest")

oi.line(x="date", y="open_interest", source=dt_source)
oi.title_text_font_size = value("12pt")
oi.ygrid.grid_line_color = None
oi.yaxis.minor_tick_line_color = None
oi.xgrid.grid_line_dash = "dashed"

#adjust what information you get when you hover over it
oi_tooltips = """
<span face="font-family: Arial, Helvetica, sans-serif">
<span style="font-size: 15px;">@contract</span>&nbsp;
<span style="font-size: 10px; color: #666;">@signal</span>
<div style="line-height: 1;">
<span style="font-size: 10px; color: #666; white-space:pre;">Open Interest &#9; @open_interest{1,1}</span>
<div style="line-height: 1;">
<span style="font-size: 10px; color: #666; white-space:pre;">Date &#9; &#9; &#9; @date</span>
oi_hover = HoverTool(tooltips=oi_tooltips)

The issue is with the date (
Date &#9; &#9; &#9; @date
), it is being presented in the hover tool as epoch time.

I have tried:

  • @date

  • @date{date}

  • @date{datetime}

  • @date{dd/mm/yyyy}

  • @date{"dd/mm/yyyy"}

I would like to know the formatting options for date types using the hover tool in the html. Specifically formatting for either of the following:

  • yyyy-mm-dd

  • dd/mm/yyyy

is using data pulled from Postgresql, the procedure it is calling is returning a date type. Running

0 2016-01-26
1 2016-01-19
2 2016-01-12
3 2016-01-05


As at 4th Feb, 2016, there is no quick way possible, and an issue is currently open requesting the feature to format date/time values in the hover tool.

A workaround is also given on the issue page that can currently be used:

To get around this, I need to add a ColumnDataSource with a single field, like so:

source = ColumnDataSource(data=dict( time=df.map(lambda x: x.strftime('%d-%m-%Y')) ))

and then I tell my tooltip to display ("time", "@time").