Skip to content

Interactive fails for non-zero decimal default argument #3102

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

Closed
ianhi opened this issue Feb 7, 2021 · 8 comments · Fixed by #3113
Closed

Interactive fails for non-zero decimal default argument #3102

ianhi opened this issue Feb 7, 2021 · 8 comments · Fixed by #3113
Assignees
Labels
Milestone

Comments

@ianhi
Copy link
Contributor

ianhi commented Feb 7, 2021

Description

On master if you have a default argument that has a non-zero decimal then interactive will fail to generate a slider.
This appears to be a regression from the 7.x branch.
image

Reproduce

from ipywidgets import interactive

# only a non-zero decimal will fail
def f(tau=1, beta=1.0,  zeta=1.5):
    print(beta, tau)
interactive(f)
Jupyter Log Console
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/mambaforge/envs/widgets-2/lib/python3.9/site-packages/traitlets/traitlets.py in validate(self, obj, value)
   2093         try:
-> 2094             value = float(value)
   2095         except Exception:

TypeError: float() argument must be a string or a number, not 'NoneType'

During handling of the above exception, another exception occurred:

TraitError Traceback (most recent call last)
~/Documents/oss/jupyter/ipywidgets/ipywidgets/widgets/widget.py in _handle_msg(self, msg)
628 if 'buffer_paths' in data:
629 _put_buffers(state, data['buffer_paths'], msg['buffers'])
--> 630 self.set_state(state)
631
632 # Handle a state request.

~/Documents/oss/jupyter/ipywidgets/ipywidgets/widgets/widget.py in set_state(self, sync_data)
512 from_json = self.trait_metadata(name, 'from_json',
513 self._trait_from_json)
--> 514 self.set_trait(name, from_json(sync_data[name], self))
515
516 def send(self, content, buffers=None):

~/mambaforge/envs/widgets-2/lib/python3.9/site-packages/traitlets/traitlets.py in set_trait(self, name, value)
1434 (cls.name, name))
1435 else:
-> 1436 getattr(cls, name).set(self, value)
1437
1438 @classmethod

~/mambaforge/envs/widgets-2/lib/python3.9/site-packages/traitlets/traitlets.py in set(self, obj, value)
576
577 def set(self, obj, value):
--> 578 new_value = self._validate(obj, value)
579 try:
580 old_value = obj._trait_values[self.name]

~/mambaforge/envs/widgets-2/lib/python3.9/site-packages/traitlets/traitlets.py in _validate(self, obj, value)
608 return value
609 if hasattr(self, 'validate'):
--> 610 value = self.validate(obj, value)
611 if obj._cross_validation_lock is False:
612 value = self._cross_validate(obj, value)

~/mambaforge/envs/widgets-2/lib/python3.9/site-packages/traitlets/traitlets.py in validate(self, obj, value)
2094 value = float(value)
2095 except Exception:
-> 2096 self.error(obj, value)
2097 return _validate_bounds(self, obj, value)
2098

~/mambaforge/envs/widgets-2/lib/python3.9/site-packages/traitlets/traitlets.py in error(self, obj, value, error, info)
688 e = "The '%s' trait expected %s, not %s." % (
689 self.name, self.info(), describe("the", value))
--> 690 raise TraitError(e)
691
692 def get_metadata(self, key, default=None):

TraitError: The 'value' trait of a FloatSlider instance expected a float, not the NoneType None.

Expected behavior

A working slider is generated

Context

  • ipywidgets version 8.0.0a0 21c2dc6
@ianhi ianhi added the bug label Feb 7, 2021
@ianhi ianhi added this to the 8.0 milestone Feb 7, 2021
@davidbrochart davidbrochart self-assigned this Feb 10, 2021
@davidbrochart
Copy link
Member

Also, the initial values of the sliders are not the default values of the function's parameters. Is that expected?

@SylvainCorlay
Copy link
Member

cc @ibdafna

@davidbrochart
Copy link
Member

It's not specific to interactive, it also affects a FloatSlider:

from ipywidgets import FloatSlider
FloatSlider(value=1.5, description='zeta', min=-1.5, max=4.5)

@ibdafna
Copy link
Member

ibdafna commented Feb 10, 2021

@davidbrochart I see you self assigned, let me know if you need help!

@davidbrochart
Copy link
Member

For now I have no idea what's going on, so no problem if you want to look into it too 😄

@davidbrochart
Copy link
Member

I can see that here:

this.handleSliderChanged(values, handle);

we have values = [NaN]. Where is this value set?

@ibdafna
Copy link
Member

ibdafna commented Feb 11, 2021

I've done some tests and this combination of parameters seems to fail noUiSlider at the source😱. You can see the issue here; open the console and try dragging the slider: https://jsfiddle.net/itayd/aoL43yn8/2/

Removing the built-in format function does seem to solve the problem, but we rely on it for float precision...I'll need to dig deeper to find a suitable fix.

@davidbrochart
Copy link
Member

It is because here:

format: {
from: (value: number): number => value,
to: (value: number): number => value
}

from receives a string and should return a number. If you replace with the following it works fine:

from: (value) => Number(value),

I'll open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants