How can I replace part of a panel via a selection change in a Bokeh figure?

When you replace pnl.objects[0] no event is triggered. But if you replace pnl[0] an event is triggered.

Try the below

from bokeh.plotting import figure
from bokeh.sampledata.iris import flowers
from bokeh.models import ColumnDataSource
import panel as pn

pn.extension()

def create_figure(width):
    p = figure(height=200, width=width, tools='box_select')
    p.circle("petal_length", "petal_width", source=ColumnDataSource(flowers))
    # note: if one directly returns "p" here, it is transformed into Bokeh(Figure) only the first time
    return pn.pane.Bokeh(p)

pnl = pn.Row(create_figure(width=200))

def replace_plot(event):
    width = int(event.new)
    print("Replacing {}".format(str(pnl.objects[0])))
    pnl[0] = create_figure(width)

select = pn.widgets.RadioButtonGroup(name='Plot selection', options =['200', '400'])
select.param.watch(replace_plot, 'value')

col = pn.Column(pnl,select)
col.servable()
2 Likes