P.x_range and RangeTool

Hello, please give me some advice. Why does p_line1.x_range = Range1d(0, x1.shape[0]) and RangeTool not work?

import os
import random
import numpy as np
import pandas as pd
from bokeh.io import curdoc
from bokeh.models import Panel,ColumnDataSource, Range1d,RangeTool,MultiLine,HoverTool, Select, Button
from bokeh.plotting import figure
from bokeh.layouts import column, row

hover = HoverTool(tooltips=[
    ("index", "$index"),
    ("(x,y)", "($x, $y)"),])
tools_slef = [hover, 'box_select,xwheel_zoom,xpan,xwheel_pan,undo,redo,reset']

p_line1 = figure(tools=tools_slef,plot_height=600,plot_width=643)
# p_line1.x_range = Range1d(0, 100)
select_slide_1 = figure(plot_height=50, plot_width=643, y_axis_type=None,
                tools="", toolbar_location=None, background_fill_color="#efefef") 

button_delete_label = Button(label="delete_label", button_type="success",width=300) 
def delete_label():
    x1 = np.linspace(0, 99, 100)
    y1 = np.array([random.randint(-10, 10) for _ in range(100)])
    p_line1.x_range = Range1d(0, x1.shape[0])
    source_line1 = ColumnDataSource(data = dict(x=x1, y=y1))
    p_line1.line('x', 'y', line_color="navy", line_width=1, source=source_line1)

    select_slide_1.x_range = Range1d(0, x1.shape[0])
    select_slide_1.line(x='x', y = 'y', source=source_line1,alpha=0.6, line_color="navy",line_width=0.5)
    range_tool1 = RangeTool(x_range = p_line1.x_range)
    select_slide_1.add_tools(range_tool1)
    p_line1.toolbar.active_multi = range_tool1
button_delete_label.on_click(delete_label) 

curdoc().add_root(column(p_line1,select_slide_1,button_delete_label))

For things to by automatically synchronized, the plot and the range tool have to share the same range object. But you are doing this:

So the range tool is acting on a range object that has no relation to the plot (so there is no synchronization). You need to make sure the range tool is configured with the actual range that the plot has.

As an aside, I would say in general that you are doing too much work. Best practice with Bokeh is always to make the smallest change possible. In practice, this means, e.g. updating the start/end of the existing range

p_line1.x_range.start = 0
p_line1.x_range.end = x1.shape[0]

rather than replacing range objects wholesale. I also think it is suspect to create a range tool in a button callback. That means you are creating a new range tool on every button press. Typically, you would create tools only once, up front.

Your suggestion is very useful. Thank you.