Skip to main content

Output field

note

For larger sections of text in the parametrization, see Text

An OutputField can be used to display a textual or numeric value to the user within a parametrization that cannot be modified by the user. The value must be set in the parametrization and can be a static value or obtained dynamically.

import viktor as vkt


class Parametrization(vkt.Parametrization):
output = vkt.OutputField('Output', value=3.1415)
note

The value of the OutputField field is not included in the params.

Dynamic value

Depending on another field

If the value depends on another field, the easiest solution is to use a Lookup:

import viktor as vkt


class Parametrization(vkt.Parametrization):
param_x = vkt.NumberField('X')
output = vkt.OutputField('X (read only)', value=vkt.Lookup('param_x'))

Inside a DynamicArray

If the value depends on the result of another field within a DynamicArray, a RowLookupcan be used. Using the RowLookup, the value of a field in a specific row can be made read-only:

import viktor as vkt


class Parametrization(vkt.Parametrization):
array = vkt.DynamicArray('Array')
array.param_x = vkt.NumberField('X')
array.output = vkt.OutputField('X (read only)', value=vkt.RowLookup('param_x'))

Using a function

The most generic way to dynamically set the value is to use a callback function. Upon evaluation of the value, the platform passes the params, entity_id, entity_name, and workspace_id (>= v14.7.1) to the custom function:

import viktor as vkt


def sum_x_y(params, **kwargs):
return params.param_x + params.param_y


class Parametrization(vkt.Parametrization):
param_x = vkt.NumberField('X')
param_y = vkt.NumberField('Y')
output = vkt.OutputField('X + Y', value=sum_x_y)

If the OutputField is defined within a DynamicArray, make sure that the callback function returns a list of 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 field will be empty:

import viktor as vkt


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


class Parametrization(vkt.Parametrization):
array = vkt.DynamicArray('Array')
array.param_x = vkt.NumberField('X')
array.param_y = vkt.NumberField('Y')
array.output = vkt.OutputField('X + Y', value=sum_x_y)
tip

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

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

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