Get the Values of a Rangeslider

Hey everybody,
i try to use a RangeSlider. But sadly it does not work because python says: “‘tuple’ object has no attribute ‘value’”
When I Use ‘Slider.value’ outside the function it works well. My Code is:

import holoviews as hv
import panel as pn
from panel import widgets as pnw
import numpy as np
hv.extension('bokeh')
coords = np.random.rand(50,2)
points = hv.Points(coords)
Slider = pnw.RangeSlider(start=0, step=0.1, end=1, name='Zeitraum')

@pn.depends(Slider)
def Ensemble(Slider):
    points=hv.Points(coords)
    points.opts(color='k', marker='+', xlim=Slider.value)
    return pn.Row(points)
pn.Row(Ensemble, Slider)

Hopefully I just need (and get) a small hint.
Thanks,
Fabian

Hi @sunny,

I think to get the value inside you need to pass Slider into the def, so in

pn.Row(Ensemble, Slider)

You could replace with

pn.Row(Ensemble(Slider), Slider)

But I don’t think that’s the end of the issue, it doesn’t seem to update on slider change

Hi @sunny

I couldn’t really tell you the in’s and out’s of why some parts work and don’t but after some jigging around I decided to fall back to some stuff I know kind of works… Used this page as a starting point https://panel.holoviz.org/user_guide/Param.html

From that I took your code, threw it into a class with param instead of widget and added it into a serv-able app. Seems to do what you’d like. Hope it helps

import holoviews as hv
import panel as pn
from panel import widgets as pnw
import numpy as np
import param

pn.extension()

class CARL(param.Parameterized):

    slider = param.Range(default=(0.5, 5.73), bounds=(0, 10))
    coords = np.random.rand(50,2)
    
    @param.depends('slider')
    def view(self):
            points=hv.Points(self.coords)
            points.opts(xlim=self.slider)
            return pn.Row(points)

carl = CARL(name='CARLOSTESTING')
app = pn.Row(carl.param, carl.view)
app.show()

Cheers, Carl.

2 Likes

Carl’s approach is good too, but if you want to stay closer to your original example, the key is that inside the function, there’s no slider, just a value, so you don’t have to ask for the value. (The error message indicates that it’s already a tuple, not a slider whose value you need to request.) Panel’s depends decorator is already taking care of pulling that value out and supplying it to the function. To make that clear, I’ve renamed your Slider and Ensemble names to be lower case (always reserving upper case for class names for clarity), and also renamed Slider inside the function so that you can distinguish between the slider widget object (now called slider) and the value of the slider (slider.value, provided by Panel as a tuple called xrange to the ensemble function). Hope this helps!

import holoviews as hv
import panel as pn
from panel import widgets as pnw
import numpy as np
hv.extension('bokeh')
coords = np.random.rand(50,2)
points = hv.Points(coords)
slider = pnw.RangeSlider(start=0, step=0.1, end=1, name='Zeitraum')

@pn.depends(xrange=slider)
def ensemble(xrange):
    points=hv.Points(coords)
    points.opts(color='k', marker='+', xlim=xrange)
    return pn.Row(points)
pn.Row(ensemble, slider)
2 Likes

Thank you guys. For now James answer brings light into the dark. Since I’m working on a bigger panel I think its necessery to figure out some Classes and implement carls solution. But therefore i need to improve my python skills…
Cheers Fabian

2 Likes