Skip to main content

Hide a field

Any field can be hidden (or shown) using the visible argument:

vkt.NumberField('X', visible=...)

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:

import viktor as vkt


class Parametrization(vkt.Parametrization):
param_x = vkt.BooleanField('X')
param_y = vkt.NumberField('Y', visible=vkt.Lookup('param_x'))

In case of a nested parametrization structure, the dotted path should be used:

vkt.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:

OperatorCan be used to
Andevaluate multiple operands to be True
Orevaluate if at least one operand is True
Notevaluate an operand to be False
IsEqualevaluate two operands to be equal
IsNotEqualevaluate two operands to be NOT equal
IsTrueevaluate an operand to be True
IsFalseevaluate an operand to be False
IsNotNoneevaluate 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 to False
  • param_y is not equal to 5
import viktor as vkt


_param_z_visible = vkt.And(
vkt.IsFalse(vkt.Lookup('param_x')),
vkt.IsEqual(vkt.Lookup('param_y'), 5)
)


class Parametrization(vkt.Parametrization):
param_x = vkt.BooleanField('X')
param_y = vkt.NumberField('Y')
param_z = vkt.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).

caution

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:

vkt.IsEqual(1, True)

If implicit conversion is desired, a callback function can be used.

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, entity_name, and workspace_id (>= v14.7.1) to the custom function:

import viktor as vkt


def param_y_visible(params, **kwargs):
return params.param_x


class Parametrization(vkt.Parametrization):
param_x = vkt.BooleanField('X')
param_y = vkt.NumberField('Y', visible=param_y_visible)
tip

All individual kwargs can be added explicitly in the signature if needed:

def param_y_visible(params, entity_id, entity_name, workspace_id, **kwargs):
...

When the visibility depends on data of another entity, the entity_id can be used to navigate and obtain data using the SDK API.

Fields in a DynamicArray

RowLookup

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:

import viktor as vkt


class Parametrization(vkt.Parametrization):
array = vkt.DynamicArray('Array')
array.param_x = vkt.BooleanField('X')
array.param_y = vkt.NumberField('Y', visible=vkt.RowLookup('param_x'))
caution

Note that it is currently not possible to combine a RowLookup with one of the boolean operators above. For example, the following is NOT supported:

IsEqual(RowLookup("shape"), "Circular")

Using a function

Another option is to make use of a callback function. Make sure that such functions return 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:

import viktor as vkt


def param_y_visible(params, **kwargs):
return [row.param_x for row in params.array]


class Parametrization(vkt.Parametrization):
array = vkt.DynamicArray('Array')
array.param_x = vkt.BooleanField('X')
array.param_y = vkt.NumberField('Y', visible=param_y_visible)