I have a bokeh app that lets users browse datasets. When the user selects a new dataset, the current figure is “blown away” and a new figure is created for the new dataset. When this happens, whatever tools were activated/deactivated on the plot get reset to their default values.
I have a request from a user to not have their tools reset when they change datasets. I understand how to change which tools are active/deactivated when making a new plot, but I don’t know how to get the settings of the toolbar that is getting blown away in order to copy those settings to the new toolbar.
You might be able to on the JS side by going along the lines of …
var active_tools = []
//where f is your current figure
for (var t of f.tools){
// if tool is active, add some identifying info to active_tools (using tool_name but your use case might be more complex)
if (t.active == true){
active_tools.push(t.tool_name)
}
}
// then make your new figure "new_f" etc..
//then going through each tool on the new figure, if the tool matches the criteria/identifying info in active_tools, set its active status to true, otherwise false
for (var t of new_f.tools){
if (active_tools.includes(t.tool_name){
t.active = true
}
else {
t.active = false
}
}
active is an internal property so it will definitely only be accessible/usable from the JavaScript side. If you need to communicate this back to Python (in a Bokeh server application, say) then you’ll have to pass it back explicitly through some public property (e.g. you could define a DataModel for this purpose).
e.g. data_models — Bokeh 3.4.1 Documentation The data model you create can have all the standard on_change, js_on_change callback on the properties you define.
Hello,
Maintaining the tool settings across dataset changes in your Bokeh app can be achieved by capturing the current state of the toolbar before it’s replaced with a new figure. You can retrieve the active tool settings using figure.toolbar.active_drag, figure.toolbar.active_scroll, etc., and then apply these settings to the new figure’s toolbar after creating it for the new dataset.
You can retrieve the active tool settings using figure.toolbar.active_drag, figure.toolbar.active_scroll, etc.,
@Aurorawright I do not believe that is actually true. The docs are rather clear that these properties apply to when I plot is first rendered, e.g. fot active_drag
Specify a drag tool to be active when the plot is displayed.
Looking at the actual code only seems to confirm this. It appears that the values are updated to null in a few cases, but not updated in any consistent way. I would not rely on these values accurately representing the active tool states after the initial plot render.