Skip to content

Commit

Permalink
Merge pull request #24 from ssciwr/onesided-min-max
Browse files Browse the repository at this point in the history
Implement onesided minimum/maximum rules
  • Loading branch information
dokempf authored Feb 24, 2022
2 parents 7be46b4 + 5521c71 commit d93647a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
38 changes: 36 additions & 2 deletions ipywidgets_jsonschema/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ def as_tuple(obj):
return (obj,)


def minmax_schema_rule(widget, schema):
"""This implements the minimum/maximum rules
Only used for inputs bounded from one side, as ipywidgets
has dedicated widgets for inputs bound from both sides. Defined
in a separate function, because it used twice for number and
integer widgets.
"""
if "minimum" in schema:

def min_handler(_):
if widget.value < schema["minimum"]:
widget.value = schema["minimum"]

widget.observe(min_handler, names="value")

if "maximum" in schema:

def max_handler(_):
if widget.value > schema["maximum"]:
widget.value = schema["maximum"]

widget.observe(max_handler, names="value")

return widget


class Form:
def __init__(
self,
Expand Down Expand Up @@ -227,6 +254,11 @@ def _construct_simple(self, schema, widget, label=None, root=False):
# Apply a potential default
if "default" in schema:
widget.value = schema["default"]
else:
if "minimum" in schema:
widget.value = schema["minimum"]
if "maximum" in schema:
widget.value = schema["maximum"]

# Apply potential constant values without generating a widget
if "const" in schema:
Expand Down Expand Up @@ -297,7 +329,8 @@ def _construct_number(self, schema, label=None, root=False):
label=label,
)
else:
return self._construct_simple(schema, ipywidgets.FloatText(), label=label)
widget = minmax_schema_rule(ipywidgets.FloatText(), schema)
return self._construct_simple(schema, widget, label=label)

def _construct_integer(self, schema, label=None, root=False):
# Inputs bounded only from below or above are currently not supported
Expand All @@ -312,7 +345,8 @@ def _construct_integer(self, schema, label=None, root=False):
label=label,
)
else:
return self._construct_simple(schema, ipywidgets.IntText(), label=label)
widget = minmax_schema_rule(ipywidgets.IntText(), schema)
return self._construct_simple(schema, widget, label=label)

def _construct_boolean(self, schema, label=None, root=False):
# Extract the labelling for the checkbox
Expand Down
12 changes: 12 additions & 0 deletions tests/schemas/integer-onesided-max.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"default": 42,
"schema": {
"default": 42,
"maximum": 45,
"type": "integer"
},
"valid": [
45,
42
]
}
12 changes: 12 additions & 0 deletions tests/schemas/integer-onesided-min.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"default": 42,
"schema": {
"default": 42,
"minimum": 40,
"type": "integer"
},
"valid": [
40,
45
]
}
12 changes: 12 additions & 0 deletions tests/schemas/number-onesided-maximum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"default": 42,
"schema": {
"default": 42,
"maximum": 45,
"type": "number"
},
"valid": [
45,
42
]
}
12 changes: 12 additions & 0 deletions tests/schemas/number-onesided-min.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"default": 42,
"schema": {
"default": 42,
"minimum": 40,
"type": "number"
},
"valid": [
40,
45
]
}

0 comments on commit d93647a

Please sign in to comment.