Trigger "reset figure" when user updates data

I am developing a dashboard that uses Bokeh (v. 3.4.3) for all of the plots and Panel (v. 1.4.5) for the layout and widgets. The dashboard users frequently update the data and also heavily use the zoom and panning features. When the user is zoomed in and they update the data, the x-axis doesn’t always update.

Is it possible to somehow add a command to the update_data function below that will do the same thing as pressing the reset tool. I want to do something similar to this ticket, but using a Panel button instead of a Bokeh button.

Ultimately what I’m trying to do is reset the x-axis, but in the context of my dashboard using p.x_range.start interferes with some of the other features.

Any ideas?

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
import panel as pn

pn.extension()


data = {"x": [1, 2, 3, 4, 5], "y": [4, 7, 5, 10, 2]}
source = ColumnDataSource(data=data)

p = figure(tools=("xpan", "box_zoom", "reset", "save"))

p.line(x="x", y="y", source=source)


button = pn.widgets.Button(name="Update data")


def update_data(button):
    x = [x + 3 for x in source.data["x"]]
    y = source.data["y"]
    source.data = {"x": x, "y": y}


button.on_click(update_data)

layout = pn.Column(button, p)

layout.servable()

Enter panel serve {file_name.py} in the terminal to serve the dashboard.

Currently the only way to do this is to call the reset method on the plot view, which has to be done from JavaScript (e.g. a CustomJS callback). I don’t know whether Panel supports JS callbacks or not. Perhaps @Philipp_Rudiger or @James_A_Bednar1 can comment.

This should work:

button.js_on_click(args={'fig': p}, code='Bokeh.index.find_one_by_id(fig.id).reset()')

That works beautifully! Thanks so much!

1 Like