Skip to main content

Tree type apps

More complex apps, consisting of an entity type hierarchy, can be created by choosing the 'tree' app type in the viktor.config.toml file:

app_type = 'tree'

The app.py file can quickly become cluttered with a lot of code if your app grows in size. For tree-type apps we recommend to separate the code in sub-folders. This can be done by replacing the app.py file with a file called __init__.py within a folder named app and add sub-folders per entity type. Furthermore, it might be desirable to also split the controller and parametrization classes into separate files. For a hypothetical app with entity types BlueType and GreenType this looks as follows:

my-folder
├── app
│ ├── blue_type
│ │ ├── __init__.py
│ │ ├── controller.py
│ │ └── parametrization.py
│ ├── green_type
│ │ ├── __init__.py
│ │ ├── controller.py
│ │ └── parametrization.py
│ └── __init__.py <- this is the new entry point of your app, replacing app.py
├── requirements.txt
└── viktor.config.toml
note

The __init__.py files in the entity type sub-folders are required to make Python treat directories containing the file as packages and can be left empty.

Importing controllers

If the controller class is in a separate file, it can be imported in the app's __init__.py file using Python's import command. For example, a controller class named BlueType in a file blue_type/controller.py can be imported in __init__.py as follows:

from .blue_type.controller import BlueType  # defines entity type 'BlueType'

If the class name itself does not equal the entity type name, you can import as an alias. For example, a class named Controller in a file blue_type/controller.py can be imported in the app's __init__.py file to act as the controller for the BlueType entity type as follows:

from .blue_type.controller import Controller as BlueType  # defines entity type 'BlueType'