Skip to main content

The controller class

New in v12.11.0

Create a views-only entity type by omitting the parametrization attribute on the corresponding Controller class

Changed in v12.6.0

Controller attribute summary is no longer required to be defined.

Changed in v12.3.0

Entity type information (label, children, and show_children_as) are moved from the manifest to the Controller.

A controller forms the interface for an entity type. It defines which methods can be called on the entity type (the entry points) and returns the appropriate results. This varies from the layout of the editor (visualisation active or not) to specific visualisations or calculations.

The following attributes are necessary and should be added directly underneath your controller definition:

  • label: defines the entity type label, which is shown in the interface.

Apart from these required attributes, a Controller may have the following optional attributes and methods:

  • parametrization: defines the input parameters used for this entity. These are defined in a class inheriting from Parametrization.
  • summary: defines how the summary for this entity type is constructed.
  • children: defines which entity types are its children. Not providing the key is equal to having no children.
  • show_children_as: defines how to visualize the children ('Table' | 'Cards').
  • methods for action buttons (e.g. a download button).
  • methods for views.
  • method for processing of a file upload.
  • any other methods you'd like to create yourself.

The following example controller implements a download, a file-upload, and two views (3D geometry and data view):

from viktor import ViktorController, ParamsFromFilefrom viktor.geometry import Point, Spherefrom viktor.result import DownloadResultfrom viktor.views import GeometryView, GeometryResult, DataView, DataResult, \    DataGroup, DataItemfrom .parametrization import Parametrizationclass ModelController(ViktorController):    label = 'Model'    parametrization = Parametrization    @GeometryView('3D Geometry', duration_guess=1)    def get_3d_view(self, params, **kwargs):        geometry = Sphere(Point(0, 0, 0), radius=10)        return GeometryResult(geometry)    @DataView('Data', duration_guess=1)    def get_data_view(self, params, **kwargs):        addition = params['x'] + params['y']        multiplication = params['x'] * params['y']        main_data_group = DataGroup(            DataItem('Data item 1', addition),            DataItem('Data item 2', multiplication),        )        return DataResult(main_data_group)    def download_file(self, params, **kwargs):        return DownloadResult('file content', 'file name')    @ParamsFromFile()    def process_file(self, file, **kwargs):        return {}