holoviz / param

Param: Make your Python code clearer and more reliable by declaring Parameters
https://param.holoviz.org
BSD 3-Clause "New" or "Revised" License
410 stars 69 forks source link

Default range of param.Range fails to update using self.param.<parameter>.default assignment #909

Closed GeoVizNow closed 4 months ago

GeoVizNow commented 4 months ago

Thanks for great software and for your efforts in adding capabilities and fixes!

ALL software version info

param: 2.0.2 panel: 1.3.8 windows 10 Enterprise

Description of expected behavior and the observed behavior

I want to update a range slider based on the data at any time available in a class built on param.Parametrized. I expect to be able to update the param.Range 'default' object with a new tuple using assigment in code. I am able to update param.Range 'bounds' but not 'default'

I have used panel.EditableFloatSlider where I am able to successfully update the 'end' parameter, but I am not sure if I can/should mix in panel widgets in a param.Prametrized class. Looking forward to hear from you.

Complete, minimal, self-contained example code that reproduces the issue

import panel as pn
import param
pn.extension()

class App(param.Parameterized):
    # Define widgets
    distance = param.Range(
        label='Distance',
        bounds=(0., 10.),
        default=(0., 10.)
    )

    def update_range_bound(self):
        self.param.distance.bounds = (-5., 23.)
    def update_range_default(self):
        self.param.distance.default = (3., 17.)

    def view():
        return self.param

app = App()

pn.Row(app)

# Try update outer boundaries
app.update_range_bound()  # This works

# Try update default range
app.update_range_default()  # This does not seem to have any effect

# I could update using:
self.distance.default = (3., 17.) 
# But this does not seem to be properly respected in an app where functions depend on this parameter.

Stack traceback and/or browser JavaScript console output

Screenshots or screencasts of the bug in action

I am adding a short animation illustrating the problem. Would be happy to hear of other ways to do solve this. 2024-02-09_00-11-30_param_issue Note that in the above animation cell 5 works as expected while cell 6 does not.

jbednar commented 4 months ago

Since you are instantiating the app object, if you want to see updates, shouldn't you be updating self.distance (the current value of distance), not self.param.distance.default (the default value of distance, that will be used in the next instantiation, not this one)?

philippjfr commented 4 months ago

Indeed, this should be:

    def update_range_default(self):
        self.distance = (3., 17.)
philippjfr commented 4 months ago

Will close for now. Happy to reopen if we're both confused about what you're trying to achieve.

GeoVizNow commented 4 months ago

Since you are instantiating the app object, if you want to see updates, shouldn't you be updating self.distance (the current value of distance), not self.param.distance.default (the default value of distance, that will be used in the next instantiation, not this one)?

Thank you @jbednar and @philippjfr for quick and to the point response with explanations! I occasionally am confused about when to use the param prefix when to update widgets, but your explanation helped, and the code I was working with now works well. Really appreciated :-)