Python callback from a ColumnDataSource

Hi,
According to Python callback for HoverTool - Community Support - Bokeh Discourse it would be possible to get a python callback on the server when a HoverTool callback is triggered. This is achieved via a ColumnDataSource.

I tried to implement it but the server callback doesn’t get called. The HoverTool JS callback gets triggered and the datasource updated.

I was wondering if the datasource needs to be attached to a renderer.

Please find below a snipet of my code

hover_details_data_source = ColumnDataSource(
    {
        ‘indices’: []
    }
)
hover_details_data_source.on_change(‘data’, on_hover_callback)

code = “”"
    console.log('indices: ’ + cb_data.index.indices)
    const indices = cb_data.index.indices
    if (indices.length){
        data_source.data[‘indices’] = cb_data.index.indices
        data_source.change.emit();
    }
“”"
callback = CustomJS(
    args={
        ‘data_source’: hover_details_data_source
    },
    code=code
)

hover = plot.select(“hover”)
hover.callback = callback

def on_hover_callback(attr, old, new):
    print(‘Here’)

Hi @Gilles please edit your post to use code formatting so that the code is intelligible (either with the </> icon on the editing toolbar, or triple backtick ``` fences around the code blocks)

In order for this Python-side callback to be triggered:

it is necessary for a real, actual assignment to be made to the .data property:

data_source.data = new_data_dict

It is not possible for Bokeh to automatically detect “in-place” changes like you are making above. This is consistent and the same across every Bokeh model and property: actual assignments are what trigger property callbacks.

The change.emit() is not relevant here. That is purely a JS-side function and has no application to Python callbacks.

Thank you very much. Appreciated your quick reply. Keep going guys.

1 Like