Skip to main content

Setting parameters with a button

Changed in v12.1.0

SetParametersResult has been renamed to SetParamsResult.

The SetParamsButton allows a user to click a button to directly set some values on the params.

Example use case

There are several use cases. One would be that you allow a user to play around with the design and always allow him to reset the values to some default value or calculated value if he makes a mistake.

One can filter entries in a table. For example in the ground layers, one could filter out all layers that are thinner than some threshold, while also allowing the user to reset the table to the start.

Implementation

  1. Add button in parametrization
  2. Implement method in controller

Adding a button in the parametrization:

from viktor.parametrization import ViktorParametrization, SetParamsButtonclass MyParametrization(ViktorParametrization):    set_params = SetParamsButton('Set params to some fixed value', method='set_params')    clear_params = SetParamsButton('Reset params', method='clear_params')

Implementing the method on the controller:

The first example would be how to set parameters, the second example is how you can clear a parameter.

  • You do not need to specify every parameter, if you do not specify a parameter it will not be altered.
  • If you on the other hand specify a parameter that has no associated field in the input specification, it will be ignored.
from viktor import ViktorControllerfrom viktor.result import SetParamsResultclass MyController(ViktorController):    ...    def set_params(self, params, **kwargs):        return SetParamsResult({            "number": 12.3,            "integer": 12,            "text": "abcd",            "textarea": "defg",            "datefield": datetime.date(2054, 1, 23),            "bool": True,            "select": "Green",            "autocomp": "Green",            "multiselect": ["Red", "Green"],            "table": [{"c1": 123, "c2": 123}, {"c1": 321, "c2": 123}],            "geopoint": GeoPoint(...),          # from viktor.geometry            "geopolyline": GeoPolyline(...),    # from viktor.geometry            "geopolygon": GeoPolygon(...),      # from viktor.geometry            "entity": API().get_entity(...),    # type viktor.api_v1.Entity        })    def clear_params(self, params, **kwargs):        return SetParamsResult({            "number": None,            "integer": None,            "text": "",            "textarea": "",            "datefield": None,            "bool": False,            "select": None,            "autocomp": None,            "multiselect": [],            "table": [],            "geopoint": None,            "geopolyline": None,            "geopolygon": None,            "entity": None,        })

In case of a nested parametrization structure, the return value is a nested dictionary:

SetParamsResult({    "tab": {        "section": {            "number": None,            ...        }    }})