Changing a plots y-axis range dynamically

hello,
(updated as Ive figured otu a few things. )
I have a plot and currently it dynamically scales the y-axis based on data. However, I would like to have it with a range of (0, 100) by default, and then have a check box, and once checked, the y-axis would then dynamically scale. I think it should look something like this:

# CPU line plot
cpu_plot = figure(plot_width=800, plot_height=300, title='CPU Utilization %')
# cpu_plot.yaxis.axis_label = 'CPU Usage in %'
cpu_ds = [0, 0, 0, 0]
for i in range(len(cpu_labels)):
    cpu_ds[i] = (cpu_plot.line(x, cpu_data[cpu_labels[i]], line_width=2,
                               color=color_list[i], legend_label=cpu_labels[i])).data_source
cpu_plot.legend.click_policy = "hide"

default_data_range = cpu_plot.y_range
cpu_plot.y_range = Range1d(0, 100) # this line updates the y_range just fine
# dynamic scaling:
def update_scaling(attr, old, new):
    print(attr, old, new)
    if new == [0]:
        cpu_plot.y_range =default_data_range #this update does nto happen
        cpu_plot.title.text = "name 1" #this update happens correctly
    else:
        cpu_plot.y_range = Range1d(0, 50) #this update does not happen
        cpu_plot.title.text = "name 2" #this update happens correctly

checkbox_labels = ["Enable Dynamic Y-axis Scaling"]
checkbox_group = CheckboxGroup(labels=checkbox_labels, active=[], css_classes=['custom_textinput'],)
checkbox_group.on_change('active', update_scaling)

for some reason, the y_range updates in the if-else statements does not happen, even though I see the title update working fine, and the y_range update outside the function works fine.

Thank you!

Hi @Juie 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)

my apologies - its fixed.

@Bryan - was wondering if you were able to give any advise on this topic. thank you!

I would recommend setting the individual start and end values on the existing range, rather than creating a whole new Range1d object. Best practice with Bokeh is always to change the smallest thing possible to accomplish what you want to achieve. Otherwise, to investigate further I would need a complete Minimal Reproducible Example that I could actually run.