Skip to main content

Set params using a button

Changed in v12.1.0

SetParametersResult has been renamed to SetParamsResult.

A set params action enables the user to click a button to directly set some or all values on the params.

Example use case

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 a SetParamsButton in the parametrization class
  2. Implement the button method on the corresponding controller class

Adding a button in the parametrization:

from viktor.parametrization import ViktorParametrization, SetParamsButtonclass Parametrization(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,            ...        }    }})