ipywidgets icon indicating copy to clipboard operation
ipywidgets copied to clipboard

math.inf / float('inf') do not work with `BoundedFloatText`

Open kdheepak opened this issue 8 years ago • 8 comments

I've tried all the following methods to create a LowerOnlyBoundedFloatText, but they all seem to throw one error or the other.

import ipywidgets as ipyw
import math
import numpy as np
import decimal

ipyw.BoundedFloatText(
            value=0,
            min=0,
            max=float('inf'), # math.inf, np.inf, decimal.Decimal('infinity'), None
            disabled=False,
        )

Any suggestions?

kdheepak avatar Jul 22 '17 06:07 kdheepak

I think it isn't implemented. If you want to submit a pr, I think supporting None might be easiest, since I don't think that inf serializes to json.

jasongrout avatar Jul 22 '17 11:07 jasongrout

@jasongrout It appears by default float('inf') is used if max is not provided – this is in the traitlets package [1].

Also, inf serializes to Infinity when using the standard library json.

import json
json.dumps({'x': float('inf')})
# '{"x": Infinity}'

And Javascript also supports Infinity. Using None may be more challenging.

kdheepak avatar Jul 25 '17 05:07 kdheepak

JSON does not support +/- infinity or NaN. They could of course be serialized to strings, but they will then require deserializing.

C.f.: https://stackoverflow.com/questions/1423081/json-left-out-infinity-and-nan-json-status-in-ecmascript

vidartf avatar Jul 25 '17 08:07 vidartf

Also c.f. the json spec: http://www.json.org/

And I tried it again in Chrome:

> JSON.parse('{"x": Infinity}')
Uncaught SyntaxError: Unexpected token I in JSON at position 6
    at JSON.parse (<anonymous>)
    at <anonymous>:1:6

jasongrout avatar Jul 25 '17 11:07 jasongrout

That said, Python does have an option to have proper IEEE float support: see the allow_nan argument at https://docs.python.org/2/library/json.html#basic-usage

jasongrout avatar Jul 25 '17 11:07 jasongrout

Just run into this issue - supporting None as an alternative would be helpful - in the current version it's possible to set the values to inf, but the widget breaks (somehow sets the value to None, guessing that the inf gets turned into a JS undefined somewhere)

mangecoeur avatar Nov 26 '21 15:11 mangecoeur

If anyone wants to implement this using None, that would likely be accepted. :)

vidartf avatar Nov 30 '21 18:11 vidartf

This is a related issue in ipykernel/jupyter_client: https://github.com/jupyter/jupyter_client/pull/708

SylvainCorlay avatar Dec 01 '21 03:12 SylvainCorlay