Skip to main content

Entity selection

When an entity depends on one or more other entities, the entity selection fields enable the user to make this selection. This is especially useful when making a tree-type app.

If none of the below entity selection fields are sufficient, data between entities can be shared using the API.

Select child(ren)

For the selection of a single child entity, the ChildEntityOptionField can be used. For example, when the entity type structure of your application looks similar to the following, and you need your users to select one of the child entities of a Project, the following code can be used:

ProjectFolder
└── Project
└── CPTFile
import viktor as vkt


class Parametrization(vkt.Parametrization):
selected_cpt = vkt.ChildEntityOptionField('Select a CPT')

In case multiple children should be selectable, the ChildEntityMultiSelectField can be used.

Select sibling(s)

Similar to children, siblings can be selected using the SiblingEntityOptionField:

import viktor as vkt


class Parametrization(vkt.Parametrization):
selected_sibling = vkt.SiblingEntityOptionField('Select a sibling')

In case multiple siblings should be selectable, the SiblingEntityMultiSelectField can be used.

Select generic entity

In some cases, both the ChildEntityOptionField and SiblingEntityOptionField may not be sufficient because the entities you need to select are in a different part of the entity type tree. In the following example, we want to select a CPTFile entity in a Foundation entity:

ProjectFolder
└── Project
├── CPTFolder
│ └── CPTFile
└── Foundation

We can make use of the EntityOptionField and the entity_type_names attribute to filter which entity type(s) should be selectable:

import viktor as vkt


class Parametrization(vkt.Parametrization):
selected_cpt = vkt.EntityOptionField('CPT File', entity_type_names=['CPTFile'])

Or use the EntityMultiSelectField when multiple entities should be selectable:

Entity object

When the user selects an entity with these fields, the entity is returned in the params as Entity object. For more detail on how to use an Entity, see this guide.

import viktor as vkt


class Parametrization(vkt.Parametrization):
cpt_entity = vkt.EntityOptionField('Select a CPT', entity_type_names=['CPTFile'])


class Controller(vkt.Controller):
parametrization = Parametrization

@vkt.DataView("CPT data")
def visualize(self, params, **kwargs):
cpt_entity = params.cpt_entity # Entity | None
if cpt_entity:
return vkt.DataResult(vkt.DataGroup(
x=vkt.DataItem('CPT X coordinate', cpt_entity.last_saved_params.x_coordinate),
y=vkt.DataItem('CPT Y coordinate', cpt_entity.last_saved_params.y_coordinate)
))
return vkt.DataResult(vkt.DataGroup())