Use of `Bokeh.documents[0]`

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 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=""
                  download target="_blank"

def printWarning(attr, old, new):
    print('Warning to sys')

# Connect callbacks
div.on_change('tags', printWarning)

# Set up layouts and add to document

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).

One item of concern: Multiple users will be using my application at the same time, and per How to access and update Bokeh plots or widgets using an external JavaScript code? - Stack Overflow, Bokeh.documents[0] only works when there’s one document in the app. However, I did test serving the application (bokeh serve example --port 343) and then opening multiple browser tabs to http://localhost:344/example, and everything seemed to work fine.


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.

Ah gotcha. Sounds like Bokeh.documents is the list of documents on the user’s browser tab.

Sounds like there shouldn’t be an issue then, so I’ll go ahead and use this method.


1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.