Numbers
Decimal number
NumberField
is a generic numeric field that can contain numbers with
decimals.
from viktor.parametrization import ViktorParametrization, NumberField
class Parametrization(ViktorParametrization):
number = NumberField('This is a NumberField')
Slider
The NumberField
can also be shown as a slider using the variant
argument:
NumberField('This is a NumberField shown as slider', variant='slider')
Integer
IntegerField
is a specific numeric field that can contain integers only.
from viktor.parametrization import ViktorParametrization, IntegerField
class Parametrization(ViktorParametrization):
integer = IntegerField('This is a IntegerField')
Setting min/max
The min and max value of the field can be set using the min
and max
arguments respectively:
NumberField('X', min=0, max=10)
When the user enters a value that exceeds these bounds, the fields are marked as invalid.
If the value for min or max needs to be calculated 'dynamically' (based on other input fields), one of the below methods can be used.
Depending on other field
If the value for min or max depends on another numeric field, the easiest solution is to use a
Lookup
. This looks up the value of the field with given name upon evaluation of
the min/max constraint:
from viktor.parametrization import ..., Lookup
class Parametrization(ViktorParametrization):
param_x = NumberField('X')
param_y = NumberField('Y', min=Lookup('param_x'))
In case of a nested parametrization structure, the dotted path should be used:
Lookup('tab.section.param_x')
When the Lookup cannot find the target field, a warning is raised and the min/max constraint is set to its default
(i.e. min=None
, max=None
).
Inside a DynamicArray
If the value for min or max depends on the result of another numeric field within a
DynamicArray
, a RowLookup
can be
used. Using the RowLookup
, a field of a specific row within the array can be made dependent of another field within
the same row:
from viktor.parametrization import ..., RowLookup
class Parametrization(ViktorParametrization):
array = DynamicArray('Array')
array.param_x = NumberField('X')
array.param_y = NumberField('Y', min=RowLookup('param_x'))
Using a function
The most generic way to dynamically set the min/max value is to use a 'callback function'. Upon evaluation of the min
or max constraint, the platform passes the params
, entity_id
, and entity_name
to the custom function:
def param_y_min(params, **kwargs):
return params.param_x
class Parametrization(ViktorParametrization):
param_x = NumberField('X')
param_y = NumberField('Y', min=param_y_min)
All individual kwargs
can be added explicitly in the signature if needed:
def param_y_min(params, entity_id, entity_name, **kwargs):
...
For constraints that have to be applied to fields within a DynamicArray
, make sure that the callback function returns
a list of the constraint values, one for each row in the array. The length of this list must be equal to the number of
rows in the array, otherwise (a warning is logged and) the constraint is ignored:
def param_y_min(params, **kwargs):
return [row.param_x for row in params.array]
class Parametrization(ViktorParametrization):
array = DynamicArray('Array')
array.param_x = NumberField('X')
array.param_y = NumberField('Y', min=param_y_min)
When the value for min/max depends on data of another entity, the entity_id
can be used to share data between
entities using the API.