View interaction
View interaction can be used to let the user interact (e.g. select, move) with objects within a view to provide input. Currently, the following interactions are supported:
MapSelectInteraction
: select map features on a MapView/GeoJSONView (>= v13.2.0)
To interact with a view, the interaction
argument can be used on any of the
action buttons. The interaction
argument allows
for any view interaction (see above for all possible interactions) and specifies the view to interact with (among other
interaction dependent parameters). The corresponding button method is triggered after the user finishes the
interaction, and the corresponding InteractionEvent
is passed along in the
method's parameters under the keyword event
.
MapView/GeoJSONView
Currently, the following interaction types exist on the MapView
and GeoJSONView
(and corresponding MapAndDataView
and GeoJSONAndDataView
):
Select
New in v13.2.0
The MapSelectInteraction
can be used to allow for the selection of
map features on a map view. Beside the (obligatory) view
method, the following optional arguments can be set:
selection
: specify 1 or more selection groups that the user can select from. If omitted, the user can select from all features that haveidentifier
set. All group names added toselection
must have been pre-defined in the view method return value under theinteraction_groups
argument (e.g.MapResult(..., interaction_groups={...}
).min_select
: specify the minimum number of features a user can select (>= 1, default: 1)max_select
: specify the maximum number of features a user can select (>= min_select, default: no limit)
An example of a MapView
that defines specific interaction_groups
is shown below. Note that setting
interaction_groups
is optional; all features with identifier
set can be interacted with regardless:
class MyController(ViktorController): ... parametrization = MyParametrization @MapView('Map', 2) def get_map_view(self, params, **kwargs): point_1 = MapPoint(..., identifier="point1") point_2 = MapPoint(..., identifier="point2") line_1 = MapLine(..., identifier="line1") polygon_1 = MapPolygon(..., identifier="polygon1") my_interaction_groups = { 'points': [point_1, point_2], # or point to the identifier: ['point1', 'point2'] 'lines': [line_1], # or point to the identifier: ['line1'] } return MapResult(..., interaction_groups=my_interaction_groups)
An example parametrization that enables interaction on the above defined MapView can look as follows. Note that the
interaction can, but does not have to make use of the pre-defined interaction_groups
on the view by means of the
selection
argument:
from viktor.parametrization import MapSelectInteractionclass MyParametrization(ViktorParametrization): # Select from all map features with `identifier` defined, ignoring any `interaction_groups` on MapResult. # Min. select = 2, max. select = 3. button_1 = DownloadButton(..., method='select_on_map', interaction=MapSelectInteraction('get_map_view', min_select=2, max_select=3)) # Select from the groups 'points' and 'lines' only ("polygon1" cannot be selected) pre-defined on MapResult. # Min. select = 1 (default), no maximum. button_2 = DownloadButton(..., method='select_on_map', interaction=MapSelectInteraction('get_map_view', selection=['points', 'lines']))
The event
passed along in the button's method parameters has the following attributes:
event.type
: 'map_select'event.value
:List[Union[str, int]]
= list of identifiers of selected features
from viktor.views import InteractionEvent # for type-hinting purposesclass MyController(ViktorController): ... def select_on_map(self, params, event: Optional[InteractionEvent], **kwargs): if event: # event will never be None if this method is only called by button(s) with interaction print(f"Triggered '{event.type}' interaction event with the following value: {event.value}") # do something with selected features in event.value, e.g. report = create_report_of_selected_features(event.value) return DownloadResult(report, "my_report.pdf") else: # None print("No interaction event triggered.")