Skip to main content

Set params using a button

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, SetParamsButton


class 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 ViktorController
from viktor.result import SetParamsResult

class MyController(ViktorController):
...

def set_params(self, params, **kwargs):
return SetParamsResult({
"number": 12.3,
"integer": 12,
"text": "abcd",
"textarea": "defg",
"date": 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
"color": Color(...), # from viktor.core (>= v14.0.0)
})

def clear_params(self, params, **kwargs):
return SetParamsResult({
"number": None,
"integer": None,
"text": "",
"textarea": "",
"date": None,
"bool": False,
"select": None,
"autocomp": None,
"multiselect": [],
"table": [],
"geopoint": None,
"geopolyline": None,
"geopolygon": None,
"entity": None,
"color": None,
})

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

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