'Tags' or 'name' property of image accessed by CustomJS callbacks

From the documents for bokeh.plotting.figure.Figure.image, under
tags’ it reads:

Or simply a convenient way to attach any necessary metadata to a model that can be accessed by CustomJS callbacks, etc.

How can I access this ‘tags’ within a CustomJS callback? Any hint is appreciated. Thanks!

@thisuser

If the CustomJS callback is associated with the model that triggers the callback, you can access the tags via cb_obj.tags. The following is bokeh’s example modified to log the tags property to the browser JavaScript console in that scenario.

If you want to access the tags property of another model, simply pass that model via the args argument to CustomJS and access it through the passed model.

See the User’s documentation of CustomJS callbacks for more information, here.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
"""
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS, Slider
from bokeh.plotting import Figure, output_file, show

output_file("js_on_change.html")

x = [x*0.005 for x in range(0, 200)]
y = x

source = ColumnDataSource(data=dict(x=x, y=y))

plot = Figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

callback = CustomJS(args=dict(source=source), code="""
    var data = source.data;
    var f = cb_obj.value
    var x = data['x']
    var y = data['y']
    for (var i = 0; i < x.length; i++) {
        y[i] = Math.pow(x[i], f)
    }
    source.change.emit();
    
    console.log("CustomJS [INFO] " + cb_obj.tags)
""")

slider = Slider(start=0.1, end=4, value=1, step=.1, title="power", tags=["slider info"])
slider.js_on_change('value', callback)

layout = column(slider, plot)

show(layout)
1 Like