Dynamic widget values

Download this notebook from GitHub (right-click to download).


In [ ]:
import panel as pn

pn.extension()

This example demonstrates how to control one set of widgets with another set of widgets, such as when the value of one widget changes the allowable values of another. Here the title_widget and value_widget control the title and ranges of the other set of widgets, respectively.

In [ ]:
title_widget = pn.widgets.TextInput(name='This controls labels', value='LABEL TEXT')
value_widget = pn.widgets.IntSlider(name='This controls values', start=0, end=10, value=5)

meta_widgets = pn.WidgetBox(
    title_widget,
    value_widget,
)

widgets = pn.WidgetBox(
    pn.widgets.TextInput(),
    pn.widgets.Spinner(),
    pn.widgets.IntSlider(),
    pn.widgets.RangeSlider(),
    pn.widgets.FloatSlider(),
)

def update_titles(event):
    for w in widgets:
        w.name = '%s %s' % (w.__class__.__name__, event.new)

title_widget.param.watch(update_titles, 'value')
title_widget.param.trigger('value')

def update_values(event):
    for w in widgets:
        if isinstance(w.value, (int, float)):
            w.value = event.new
            w.end = event.new

value_widget.param.watch(update_values, 'value')

pn.Row(meta_widgets, widgets).servable()

Download this notebook from GitHub (right-click to download).