Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spinner widget doesn't honor bounds when created from Param object #2740

Closed
sdc50 opened this issue Sep 16, 2021 · 3 comments · Fixed by #2775
Closed

Spinner widget doesn't honor bounds when created from Param object #2740

sdc50 opened this issue Sep 16, 2021 · 3 comments · Fixed by #2775
Labels
type: bug Something isn't correct or isn't working
Milestone

Comments

@sdc50
Copy link
Contributor

sdc50 commented Sep 16, 2021

The Spinner widget allows you to set a start and end bound. The widget does not let a user set the number outside the bounds:

pn.widgets.Spinner(start=0, end=5)

When creating a Spinner widget from a param.Number I would have expected the start and end to be derived from the bounds of the param object.

class Demo(param.Parameterized):
    a = param.Number(default=5, bounds=(0, 5))
    
    
    def panel(self):
        return pn.widgets.Spinner.from_param(self.param.a)

However, in this case if a user tries to specify a number outside of the bounds the widget allows it and then it raises an error:

Traceback (most recent call last):
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/pyviz_comms/__init__.py", line 325, in _handle_msg
    self._on_msg(msg)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/panel/viewable.py", line 273, in _on_msg
    doc.unhold()
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/bokeh/document/document.py", line 669, in unhold
    self._trigger_on_change(event)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/bokeh/document/document.py", line 1180, in _trigger_on_change
    self._with_self_as_curdoc(event.callback_invoker)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/bokeh/document/document.py", line 1198, in _with_self_as_curdoc
    return f()
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/bokeh/util/callback_manager.py", line 161, in invoke
    callback(attr, old, new)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/panel/reactive.py", line 301, in _comm_change
    self._process_events({attr: new})
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/panel/reactive.py", line 262, in _process_events
    self.param.set_param(**self_events)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/param/parameterized.py", line 1526, in set_param
    self_._batch_call_watchers()
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/param/parameterized.py", line 1665, in _batch_call_watchers
    self_._execute_watcher(watcher, events)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/param/parameterized.py", line 1627, in _execute_watcher
    watcher.fn(*args, **kwargs)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/panel/param.py", line 438, in link_widget
    self.object.param.set_param(**{p_name: change.new})
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/param/parameterized.py", line 1519, in set_param
    setattr(self_or_cls, k, v)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/param/parameterized.py", line 316, in _f
    instance_param.__set__(obj, val)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/param/parameterized.py", line 318, in _f
    return f(self, obj, val)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/param/__init__.py", line 624, in __set__
    super(Dynamic,self).__set__(obj,val)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/param/parameterized.py", line 318, in _f
    return f(self, obj, val)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/param/parameterized.py", line 940, in __set__
    self._validate(val)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/param/__init__.py", line 908, in _validate
    self._validate_bounds(val, self.bounds, self.inclusive_bounds)
  File "~/miniconda/envs/myenv/lib/python3.7/site-packages/param/__init__.py", line 872, in _validate_bounds
    "not %s." % (self.name, vmax, val))
ValueError: Parameter 'a' must be at most 5, not 6.

image

Even if you manually set the start and end values in the from_param call you get the same error:

class Demo(param.Parameterized):
    a = param.Number(default=5, bounds=(0, 5))
    
    
    def panel(self):
        return pn.widgets.Spinner.from_param(self.param.a, start=self.param.a.bounds[0], end=self.param.a.bounds[1])

It seems like the start and end values are being passed through:

image

But if you set start and end after the widget is created then it works as expected:

image

@sdc50
Copy link
Contributor Author

sdc50 commented Sep 16, 2021

This probably goes without saying, but I would expect the Spinner object to honor the softbounds if it is specified, otherwise it should honor the bounds.

@jbednar
Copy link
Member

jbednar commented Sep 17, 2021

When creating a Spinner widget from a param.Number I would have expected the start and end to be derived from the bounds of the param object.

I agree; that sounds like a bug!

This probably goes without saying, but I would expect the Spinner object to honor the softbounds if it is specified, otherwise it should honor the bounds.

Softbounds were designed for a slider widget where some bounds are required before displaying anything. For a spinner widget, I would expect softbounds to be ignored; they are only hints, and if the user wants the value to go outside the soft bounds, why not let them?

@philippjfr philippjfr added the type: bug Something isn't correct or isn't working label Sep 20, 2021
@philippjfr philippjfr added this to the v0.12.4 milestone Sep 20, 2021
@maximlt
Copy link
Member

maximlt commented Sep 24, 2021

As a workaround you could use pn.widgets.IntInput.from_param(...) instead of pn.widgets.Spinner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't correct or isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants