My code creates a hyperlink in a Bokeh Div. I’d like to generate a callback that runs when the user clicks the hyperlink. There’s no Bokeh event-type for this kind of user interaction, but I was reviewing these message boards, and I saw that I could do the following:
from bokeh.io import curdoc
from bokeh.models import Button, Div
# Set up widgets
button = Button(label='Click me', name='myButton')
div = Div(name='myDiv')
# Callbacks
def createHyperlink():
div.text = """<html><head/><body>
<a href="www.google.com"
download target="_blank"
onclick="Bokeh.documents[0].get_model_by_name('myDiv').tags=['test'];
Bokeh.documents[0].get_model_by_name('myDiv').tags=[]"
>google.com</a></body></html>"""
def printWarning(attr, old, new):
print('Warning to sys')
# Connect callbacks
button.on_click(createHyperlink)
div.on_change('tags', printWarning)
# Set up layouts and add to document
curdoc().add_root(button)
curdoc().add_root(div)
Now, whenever the user clicks the hyperlink, the tag of the Bokeh model is changed, which runs the printWarning callback I want to be run.
I’m just wondering whether or not this is good practice, or if there might be some issues with this method that I’m not aware of (I don’t see it in the documentation; I only see it on message boards).
FYI this will nearly always be the case for Bokeh server apps. I think you would have to embed multiple app sessions in a single page using server_document or server_session in order for it not to be the case.