pythonpyvizpanel-pyviz

Return the value of Panel's slider widget


I'm using Panel's slider widget to inspect some data and I want to use the values of this widget as an argument of other functions. I built my own code based on this one:

import hvplot.pandas
import panel as pn
import holoviews as hv

from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature

pn.extension()

bins = pn.widgets.IntSlider(name='Number of bins', start=5, end=25, step=5, value=10)
kde = pn.widgets.Checkbox(name='Show density estimate')
observations = pn.widgets.Checkbox(name='Show individual observations')
bandwidth = pn.widgets.FloatSlider(name='KDE Bandwidth', start=0.1, end=1)

@pn.depends(bins.param.value, kde.param.value,
            observations.param.value, bandwidth.param.value)
def get_plot(bins, kde, obs, bw):
    plot = sea_surface_temperature.hvplot.hist(bins=bins, normed=True, xlabel='Temperature (C)', padding=0.1)
    if kde:
        plot *= sea_surface_temperature.hvplot.kde().opts(bandwidth=bw)
    if obs:
        plot *= hv.Spikes(sea_surface_temperature, 'temperature').opts(
            line_width=0.1, alpha=0.1, spike_length=-0.01)
    return plot

widgets = pn.WidgetBox('## Sea surface temperatures', bins, observations, kde)

def add_bandwidth(event):
    if event.new:
        widgets.append(bandwidth)
    else:
        widgets.remove(bandwidth)

kde.param.watch(add_bandwidth, 'value')

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

Putting my question in the context of the previous code, I would like to use the value of bins. In order to do this, I changed the final output to:

output = (pn.Row(widgets, get_plot).servable(), bins)

Doing output[1] shows the plot, but output[2] shows the slider widget instead of the chosen integer.

If I change the output of the get_plot function to return (plot,bins) the final plot shows the widget plus the same as do print(plot) instead of the histogram.

Here's the source of the code I used to develope mine: https://panel.pyviz.org/gallery/simple/temperature_distribution.html#gallery-temperature-distribution


Solution

  • You defined bins to be the widget, so naturally it shows the widget.

    In your case, bins.value is probably what you're looking for, following the docs here: https://panel.pyviz.org/user_guide/Widgets.html