Hi,
I discover Bokeh one month ago, Wonderful! Except one thing that I cannot solve :
I’d like to reset all my plots simultaneously with exactly the same way than the “ResetTool” button.
So i need one intruction able to copycat it for each figure…
I saw on internet something like : myfigure.reset.emit() but doesn’t work
any ideas ?
Thanks,
There is not an especially good way to reset plots programmatically present. You would need to call the reset method on the plot view (that is what the reset tool does). Bt view objects are not as easily accessible. There is a window global Bokeh.index that holds all view objects, you will just have to root around that object to find the views that you want to call reset on.
Also, just in case it’s not evident: the above discussion regards JavaScript. There is no direct way to do this from Python, you would need to trigger a JS callback or CustomAction to do what is described above.
from bokeh.layouts import row
from bokeh.models import Button, CustomJS
from bokeh.plotting import figure, show
p1 = figure()
p1.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7])
p2 = figure()
p2.circle([2, 5, 8, 2, 7], [1, 2, 3, 4, 5])
b = Button(label="Reset all plots")
b.js_on_click(CustomJS(code="""\
document.querySelectorAll('.bk-tool-icon-reset[title="Reset"]').forEach(d => d.click())
"""))
show(row(p1, p2, b))
One thing to note - I didn’t use Bokeh.index because it requires traversing the whole tree structure when layouts have any level of nesting (rows, columns, grids).
The method in the code block just finds the HTML elements that represent the reset buttons and clicks on them. So it will work only if there is such a button on a plot. Also, the code will break if something relevant changes in Bokeh (a different class name or a different tool name or not using the title attribute). But since the BokehJS API is not yet considered stable, the same can happen with Bokeh.index or any other method.
you can also use p1.renderers = [], where p1 would be the plot widget. Setting the renderer to an empty list basically cleans the elements of the figure. You can put this into any update function.