Favorites
PythonPart favorite files (.pyv) can contain parameters of a certain PythonPart (all of them or just a selection) together with their values. A typical use case for it is, when the user wants to place the PythonPart in DF with the same parameters over and over again, but wants to save time by not having to input the same values every time.
The three buttons on the bottom left side of a property palette are responsible for saving and loading a favorite file as well as resetting the values in the palette to the defaults defined in the .pyp file.
In a standard PythonPart this functionality works without further actions. In an Interactor the handling of favorite files must be implemented by the developer with the appropriate functions.
Favorite file in Interactor
The class BuildingElementListService provides methods for managing the favorite files. Use these methods inside special functions to properly handle the favorite files.
Example on GitHub
The complete implementation of the favorites handling in an interactor PythonPart is shown in the example Line2DInteractor ( PYP | PY)
Save favorite
To save the parameter values in the favorite file, implement the method execute_save_favorite in your interactor class. This method is called after the user have selected the saving path of the favorite file.
Example
def execute_save_favorite(self, file_name):
""" save the favorite data """
BuildingElementListService.write_to_file(file_name, self.build_ele_list)
The member function write_to_file reads the data from build_ele_list and writes it into the favorite file.
Note
Parameter persistency is being considered,
when saving the favorite file. Only parameters with the <Persistent>
tag
set to Favorite
or ModelAndFavorite
are being saved.
Read favorite
To read the parameter values from the favorite file, implement the function execute_load_favorite into your interactor class. This function is called after the user have selected the favorite file.
Example
def execute_load_favorite(self, file_name):
""" load the favorite data """
BuildingElementListService.read_from_file(file_name, self.build_ele_list)
self.palette_service.update_palette(-1, True) #(1)!
- Directly after reading the parameters the palette must be updated to see the new values
The method read_from_file reads the data from the favorite file and stores it into the build_ele_list.
Reset parameter values
When the user presses the reset button the function reset_param_values is called. To actually reset the values in the property palette to the defaults from the .pyp file, this method must be implemented into the Interactor class.
Example
def reset_param_values(self, _build_ele_list): #(1)!
""" reset the parameter values """
BuildingElementListService.reset_param_values(self.build_ele_list) #(2)!
palette_service.update_palette(-1, True) #(3)!
- Note that the old values from the property palette are being passed to the function...
- ... but we are not using them here as an argument for the reset function. We are resetting the actual building_element instead...
- ...and updating the palette directly afterwards.
Update parameter values
When the PythonPart is created as an interactor and the .pyp file is read outside the interactor in the PythonParts framework, saving and reading the favorite data is also executed in the framework.
To execute some additional functionality after reading the favorite data, the method update_after_favorite_read must be implemented into the Interactor class. This method is called after the name of the favorite file has been entered in the favorite file dialog and the data are read from the favorite file.
Example
def update_after_favorite_read(self):
"""
Update the data after a favorite read
"""
self.palette_service.update_palette(-1, True) #(1)!
- At least the update of the property palette should be done here
Read last input
It is possible to save the parameter values from the currently created PythonPart and use it as default values when the user starts it the next time. This is improves the user experience especially, when he has to start the PythonPart over and over again with just a few parameters differing. The user does not have to input all the parameters over and over, just the ones he wants to alter.
In a standard PythonPart, or in a
script object PythonPart you can enable this
behavior by setting the tag <ReadLastInput>
in the <Script>
section of the
PYP file to True:
- The default is False
From now on, after the user has created the PythonPart for the first time, a file with the
input values is created under: ...\Usr\$username$\tmp\$name_of_your_pythonpart$.pyv.
(1)
$username$
and$name_of_your_pythonpart$
are just placeholders, that in real case will be replaced by the user name and the name of your .pyp file accordingly. When workgroup manager is not active, the username will belocal
.
The next time the user start the same PythonPart, the framework gets the parameter values from this file and overrides the defaults from the PYP file. After the input is done, the framework saves the file again.
The file is saved in the user location. No directory tree is created - if two PythonParts
have the same name, one .pyv
file will be created
In an interactor PythonPart you can implement this behavior in two steps:
-
Save the values at the end of the input
Use the method write_to_default_favorite_file to save the input values into a file at the end of the user interaction i.e., in the
__del__
method of your interactor class:The file is created under:
...\Usr\___username___(1)\tmp\___name_of_your_pythonpart___.pyv.
- When no workgroup manager is used, the username will be
local
.
- When no workgroup manager is used, the username will be
-
Read the values when the input is started
This will be done by the framework, when you set the tag
<ReadLastInput>
in the<Script>
section of the PYP file to True: