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
- Add a
SetParamsButton
in the parametrization class - Implement the button method on the corresponding controller class
Adding a button in the parametrization:
import viktor as vkt
class Parametrization(vkt.Parametrization):
set_params = vkt.SetParamsButton('Set params to some fixed value', method='set_params')
clear_params = vkt.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.
import viktor as vkt
class Controller(vkt.Controller):
def set_params(self, params, **kwargs):
return vkt.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": vkt.GeoPoint(...),
"geopolyline": vkt.GeoPolyline(...),
"geopolygon": vkt.GeoPolygon(...),
"entity": vkt.api_v1.API().get_entity(...),
"color": vkt.Color(...), # (>= v14.0.0)
})
def clear_params(self, params, **kwargs):
return vkt.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:
vkt.SetParamsResult({
"tab": {
"section": {
"number": None,
...
}
}
})