Bokeh grid plots are not updating

I have a code which should update the graph, but graph is not updating


from import curdoc
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource, TextInput, Button
from bokeh.plotting import figure

# create some sample data
x = [1, 2, 3, 4, 5]
y1 = [6, 7, 2, 4, 5]
y2 = [2, 3, 4, 5, 6]
y3 = [4, 5, 6, 7, 8]

# create a column data source for the data
source = ColumnDataSource(data=dict(x=x, y1=y1, y2=y2, y3=y3))

# create the initial plot with a single column
plot1 = figure(title="Plot 1", x_axis_label="X", y_axis_label="Y")
plot1.line(x='x', y='y1', source=source)

# create a function to generate the gridplot based on the number of columns entered by the user
def generate_gridplot(num_cols):
    # create a list of plots
    plots = [plot1]
    for i in range(2, num_cols + 1):
        plot = figure(title=f"Plot {i}", x_axis_label="X", y_axis_label="Y")
        plot.line(x='x', y=f'y{i}', source=source)

    # create the gridplot with the specified number of columns
    grid = gridplot(plots, ncols=num_cols, sizing_mode='stretch_both')
    return grid

# create a text input widget and a button widget
num_cols_input = TextInput(title="Number of Columns:", value="1")
generate_button = Button(label="Generate Grid")

# create a callback function to update the gridplot based on the user's input
def update_grid():
    num_cols = int(num_cols_input.value)
    grid = generate_gridplot(num_cols)

# set the generate_button to trigger the update_grid callback function

# create the initial gridplot with a single column
grid = generate_gridplot(1)
# add the widgets and the gridplot to the document

“Dynamic” use of add_root is currently broken. See this reply for more information and workaround:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.