Reputation: 25
I am very new to Bokeh. I am trying to have clickable links on one of my plots, more precisely on the x-axis (on the ticks).
My question: is it possible to use HTML code in tick labels, for example to replace text by hypertext links?
Taking one of the examples from the documentation of Bokeh, here is my naive attempt (replacement on one of the labels, from Apples
to <a>Apples</a>
):
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral6
from bokeh.plotting import figure
output_file("bar_colors.html")
fruits = ['<a>Apples</a>', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6]
source = ColumnDataSource(data=dict(fruits=fruits, counts=counts, color=Spectral6))
p = figure(x_range=fruits, y_range=(0,9), plot_height=350, title="Fruit Counts",
toolbar_location=None, tools="")
p.vbar(x='fruits', top='counts', width=0.9, color='color', legend="fruits", source=source)
p.xgrid.grid_line_color = None
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
show(p)
It results, as you may have guessed, in a plot where I have <a>Apples</a>
written as text...
How should I proceed to have a proper hypertext link?
Upvotes: 1
Views: 508
Reputation: 34568
The short answer is no. Bokeh does it's rendering on the HTML Canvas, which is a low-level bitmapped drawing area. The HTML canvas does not directly support any DOM type elements (e.g hyperlinks) on it.
There are probably a couple of ways this might be worked around:
Bokeh is extensible You could create a custom extension that renders ticks in a completely different way, i.e. by absolutely positioning real Divs on top of the canvas instead of issuing canvas drawing commands. This would require alot of bookkeeping and precise positioning. I would not describe it as trivial, and would probably need some back and forth iteration to get working, so the public mailing list would be a better place to discuss that possibility.
There is an open issue #601
: Add support for click events on categorical axes that is relevant. If this were implemented, it would offer a similar capability (but without actual hyperlinks). It's possible this idea could be expanded to include any tick labels, not just categorical ones. The best place to provide your feedback on this potential feature is in the GitHub link.
Upvotes: 1