Using cb_obj from button.js_on_click()


From reading the doc, my understanding was that the variable “cb_obj” could be always used inside a CustomJS to reference the model that triggered the callback.

However, this doesn’t seem the case when used on a button.js_on_click callback:

from bokeh.models import Button, CustomJS
from bokeh.plotting import output_notebook, show

b = Button(label="My button")

cb1 = CustomJS(args={'b': b}, code='console.log("cb1: " + b.label)')
cb2 = CustomJS(args={}, code='console.log("cb2: " + cb_obj.label)') # Gives "undefined"
cb3 = CustomJS(code='console.log("cb3: " + cb_obj.label)') # Gives "undefined"



Am I doing something wrong here, or the “js_on_click()” doesn’t support the use of “cb_obj”?


The following seems to give access to the label in cb_obj without the requirement for additional arguments. Verified in bokeh 2.3.3.

cb3 = CustomJS(code='console.log("cb3: " + cb_obj.origin.label)')

In my experience, this is definitely different behavior than other JavaScript callbacks such as js_on_change(), where you can access properties from the cb_obj directly. That said, I do not use the JavaScript callbacks very regularly.

For what it is worth, arrived at this by inspecting the object with Object.getOwnPropertyNames(cb_obj)) in the JavaScript console to see what was actually in cb_obj. So, this is all based on experimentation and nothing in the documentation that I could see, so hopefully the developers can comment on if this is expected or not.