How to use cb_obj in callbacks in bokeh?

7k views Asked by At

In bokeh.models.actions.Action, there is a callback class for user defined callback. It passes the current plot_object as cb_obj implicitly.

However, I don't know how to access the data from the plot_object.

    fig = figure()
    fig.circle(x=[1,2,3], y=[4,5,6])
    tap_tool.action = Callback(
        code="""
            alert('clicked')
            console.log(cb_obj)

        """)

How can I access the information, e.g. x, y of a clicked circle? In a template string we can use @variable or $x to get information about each data point.

Furthermore, it seems to me that there is only 1 Circle Glyph, despite of 3 circles. So glyph has nothing to do with the number of data points, is it correct?

Does cb_obj refer to this Glyph, or the glyphRenderer that contains this glyph?

In the docs an example shows:

    var inds = cb_obj.get('selected')['1d'].indices;
    var d1 = cb_obj.get('data');

Where does select, id, indices, data come from? What's the structure of cb_obj.

2

There are 2 answers

0
bigreddot On

As of Bokeh 0.9.0, for TapTool actions, the value of cb_obj is the data source for the glyph that reported the hit. This example shows how to access the data columns:

https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html#customjs-for-tools

0
asofyan On

You can actually inspect the object through:

console.log(cb_data);
console.log(cb_obj);

For example use this callbacks to inspect the contents of both object:

scode = """
        console.log(cb_obj);
        console.log(cb_data);
        """
taptool.callback = CustomJS(args=dict(source=source),code = scode)

If you run in chrome you will see the content of cb_obj and cb_data in your logs (View-Developer-Javascripts Console)