Skip to main content

Use dynamic options in an OptionField


In case you need to create an option field with entities as options, you can make use of dedicated entity selection fields (>= v13.0.0).

New in v12.3.0

Simple options can be defined without an OptionListElement class.

Changed in v12.1.0

ToggleButton has been renamed to BooleanField.

Normally every entity has the same options, and they can be added directly inside the parametrization:

from viktor.parametrization import ViktorParametrization, OptionFieldclass ExampleParametrization(ViktorParametrization):    color = OptionField('Pick a color', options=['blue', 'red', 'green'])

Sometimes, the available options are not static however, and it is desired to make them dependent on other parameters. This can be achieved in several ways:

  • setting visibility of options
  • dynamically obtaining options using a callback function

Dynamic visibility

The following example shows how the selection of a material group can affect the available options in another selection field. By using the IsEqual operator, we can make use of the visibility to hide / show options:

from viktor.parametrization import ViktorParametrization, OptionField, OptionListElement, IsEqual, Lookupmaterial_options = [    OptionListElement('Aluminium 5083', visible=IsEqual(Lookup('material_group'), 'Aluminium')),    OptionListElement('Aluminium 6082', visible=IsEqual(Lookup('material_group'), 'Aluminium')),    OptionListElement('Steel S355', visible=IsEqual(Lookup('material_group'), 'Steel')),    OptionListElement('Steel S690', visible=IsEqual(Lookup('material_group'), 'Steel'))]class ExampleParametrization(ViktorParametrization):    material_group = OptionField('Material group', options=['Aluminium', 'Steel'])    material_type = OptionField('Material', options=material_options)

Callback function

A callback function is used in a similar way to a view method on the controller:

  • it should at least define the argument **kwargs
  • when the function is called, the following kwargs are available:
    • params can be used to make the options depend on other input parameters within the same entity
    • entity_id can be used to access other entities over the API
    • entity_name holds the name of the current entity
  • difference with view method: it does not have a self argument, because it is not part of a class

The above example on material groups can be formulated using a callback function as follows:

from viktor.parametrization import ViktorParametrization, OptionFielddef get_material_options(params, **kwargs):    if params.material_group == 'Aluminium':        return ['Aluminium 5083', 'Aluminium 6082']    elif params.material_group == 'Steel':        return ['Steel S355', 'Steel S690']    else:        return []class ExampleParametrization(ViktorParametrization):    material_group = OptionField('Material group', options=['Aluminium', 'Steel'])    material_type = OptionField('Material', options=get_material_options)

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

def get_material_options(params, entity_id, entity_name, **kwargs):

With a callback function it is also possible to make use of the entity_id to access data from another entity over the API.