Add custom tool in Bokeh Plot

Is there a way to add custom tool similar to zoom in, hover etc. I am trying to add a tool to delete/remove plot from DOM layout dynamically.

There’s an example in the documentation: https://docs.bokeh.org/en/latest/docs/user_guide/extensions_gallery/tool.html

1 Like

@p-himik any specific help in writing a new tool to delete the plot ? when click on ‘X’ icon i should be able to execute the python code to remove the plot from DOM/curdoc

what to export and extend ?
how to call a python code when clicked on ‘X’ icon on the plot tool ?

Just follow the example and export the same things. Conceptually, not name-wise.

Probably ActionTool since that’s what ResetTool extends, and that tool is rather similar to what you want.

You don’t really need to call Python code to remove a plot. All that changing Python models does is change JS models, and you can already do that from JS. But if you really want to do that in Python, I think you would need to send an event similar to ButtonClick. But I don’t know for sure - I’ve never used that functionality.

can you please share such example where i can remove a plot from JS ? if i do $('plot-1').css('display','none') or something else , i want to make sure that other plots in DOM slides-up and left as in when space frees up by deleting the plot-1

You cannot do it with just CSS because Bokeh has an internal layout system that doesn’t look at CSS.
You will have to remove the plot completely from the layout, likely either by altering the .children properties or something similar of all layout primitives like Row and Column or by recreating the whole layout from scratch. I don’t have an example at hand but I think there were some attempts either here or on Stack Overflow.