Hide a field
Any field can be hidden (or shown) using the visible
argument:
NumberField('X', visible=False)
If the visibility needs to be calculated 'dynamically' (based on other input fields), one of the below methods can be used.
Depending on BooleanField
If the visibility depends on another BooleanField
, the easiest solution is to use
a Lookup
. This looks up the value of the field with given name upon evaluation of
the visibility constraint:
from viktor.parametrization import ..., Lookup
class Parametrization(ViktorParametrization):
param_x = BooleanField('X')
param_y = NumberField('Y', visible=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 visibility constraint is set to its default
(i.e. visible=True
).
Depending on other field(s)
If the visibility depends on multiple other fields or on a field that is not a BooleanField
, the so-called 'boolean
operators' can be used. VIKTOR offers the following boolean operators:
Operator | Can be used to |
---|---|
And | evaluate multiple operands to be True |
Or | evaluate if at least one operand is True |
Not | evaluate an operand to be False |
IsEqual | evaluate two operands to be equal |
IsNotEqual | evaluate two operands to be NOT equal |
IsTrue | evaluate an operand to be True |
IsFalse | evaluate an operand to be False |
IsNotNone | evaluate an operand to be NOT None |
For example, if param_z
should be visible only when the following conditions are met:
param_x
is equal toFalse
param_y
is not equal to5
_param_z_visible = And(
IsFalse(Lookup('param_x')),
IsEqual(Lookup('param_y'), 5)
)
param_x = BooleanField('X')
param_y = NumberField('Y')
param_z = NumberField('Z', visible=_param_z_visible)
If an inconsistent type is used in a boolean operator, a warning is raised and the visibility constraint is set to its
default (i.e. visible=True
).
In Python, arbitrary types may be implicitly treated as booleans (e.g. 1 == True
evaluates to True
, as well as
bool('text')
). Note that that VIKTOR's boolean operators do not perform this implicit conversion. For example,
the following IsEqual
operator evaluates to False
:
IsEqual(1, True)
If implicit conversion is desired, a callback function can be used.
Inside a DynamicArray
If the visibility depends on the result of another 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 = BooleanField('X')
array.param_y = NumberField('Y', visible=RowLookup('param_x'))
Using a function
The most generic way to dynamically set the visibility is to use a 'callback function'. Upon evaluation of the
visibility constraint, the platform passes the params
, entity_id
, and entity_name
to the custom function:
def param_y_visible(params, **kwargs):
return params.param_x
class Parametrization(ViktorParametrization):
param_x = BooleanField('X')
param_y = NumberField('Y', visible=param_y_visible)
All individual kwargs
can be added explicitly in the signature if needed:
def param_y_visible(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_visible(params, **kwargs):
return [row.param_x for row in params.array]
class Parametrization(ViktorParametrization):
array = DynamicArray('Array')
array.param_x = BooleanField('X')
array.param_y = NumberField('Y', visible=param_y_visible)
When the visibility depends on data of another entity, the entity_id
can be used to share data between entities
using the API.