Error on the dynamic option of the OptionField example

Hi,

As I was trying to use the example for the dynamic option, I got an error when loading the app.

Any ideas?

from viktor.parametrization import Parametrization, OptionField, BooleanField

def get_color_options(params, **kwargs):
color_options = [‘blue’, ‘green’]

if params.include_red:
    color_options.append('red')

return color_options

class ExampleParametrization(Parametrization):
color = OptionField(‘Pick a color’, options=get_color_options)
include_red = BooleanField(‘Include red’)

From the looks of it there is not much wrong with the code snippets you are sharing. What is the error that you get?

Hi Raoul,

When I start the app including the example I get this error. Without the examples the app starts.

C:\Users\bart\viktor-apps\spreadsheet-test>viktor-cli start
A newer version of viktor-cli is available. Use the command ‘viktor-cli upgrade’ to upgrade from v0.13.1 to v0.13.2
2022-03-25 12:35:48.968 INFO : __ __ __ __ __
2022-03-25 12:35:48.969 INFO : \ \ / / | | | |/ /
2022-03-25 12:35:48.970 INFO : \ \ / / | | | ’ /
2022-03-25 12:35:48.970 INFO : \ / / | | | <
2022-03-25 12:35:48.971 INFO : \ / | | | .
2022-03-25 12:35:48.971 INFO : / || ||_
2022-03-25 12:35:48.972 INFO : _______ ____ _____
2022-03-25 12:35:48.972 INFO : |__ | / __ \ | __
2022-03-25 12:35:48.975 INFO : | | | | | | | |
) |
2022-03-25 12:35:48.976 INFO : | | | | | | | _ /
2022-03-25 12:35:48.978 INFO : | | | || | | | \
2022-03-25 12:35:48.981 INFO : || _
/ |_| _
2022-03-25 12:35:48.983 INFO : VIKTOR connector v5.11.1
2022-03-25 12:35:48.984 INFO : VIKTOR SDK v12.10.0
2022-03-25 12:35:48.997 INFO : Loading SDK…
2022-03-25 12:36:21.734 WARNING : /usr/src/packages/theano/scalar/basic.py:2412: DeprecationWarning: np.bool is a deprecated alias for the builtin bool. To silence this warning, use bool by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.bool_ here.
Deprecated in NumPy 1.20; for more details and guidance: NumPy 1.20.0 Release Notes — NumPy v1.23.dev0 Manual
self.ctor = getattr(np, o_type.dtype)

2022-03-25 12:36:26.203 WARNING : Using NumPy C-API based implementation for BLAS functions.
2022-03-25 12:36:31.070 WARNING : /usr/local/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ndarray size changed, may indicate binary incompatibility. Expected 16 from C header, got 88 from PyObject
return f(*args, **kwds)

2022-03-25 12:36:33.525 WARNING : /usr/src/packages/theano/tensor/basic.py:369: DeprecationWarning: np.complex is a deprecated alias for the builtin complex. To silence this warning, use complex by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.complex128 here.
Deprecated in NumPy 1.20; for more details and guidance: NumPy 1.20.0 Release Notes — NumPy v1.23.dev0 Manual
np.complex(data) # works for all numeric scalars

2022-03-25 12:36:35.205 WARNING : /usr/local/lib/python3.7/multiprocessing/process.py:297: DeprecationWarning: app.calculation_folder.controller.CalculationFolderController: Your app uses an integer type as param value for entity selection fields (ChildEntityOptionField, SiblingEntityOptionField, ChildEntityMultiSelectField, SiblingEntityMultiSelectField), which will change to an Entity type in the future. Please set viktor_convert_entity_field = True and change your code accordingly.
See upgrade instruction U65 for more detail.
self.run()

2022-03-25 12:36:35.220 ERROR : ‘function’ object has no attribute ‘_generate_entity_type’

Hi Bart, I suspect there is a public function defined in the Parametrizaion class. Could you post your complete parametrization?

Hi Kevin,

This is my complete parametrization class. Hope it helps!

Thanks!

from viktor.parametrization import Parametrization, Tab, Section, OptionField, MultiSelectField, Table
from viktor.parametrization import NumberField, LineBreak, DownloadButton, TextField, ActionButton
from viktor.parametrization import HiddenField, ChildEntityOptionField, BooleanField

class CalculationParametrization(Parametrization):

def get_color_options(params, **kwargs):
    color_options = ['blue', 'green']

    if params.include_red:
        color_options.append('red')

    return color_options

general = Tab('General')
general.object = Section('Object')
general.object.type = OptionField('Pick a color', options=get_color_options)
include_red = BooleanField('Include red')

general.equipment = Section('Equipment')
general.equipment.nl = LineBreak()
general.equipment.child_select = ChildEntityOptionField('Select a child') #, entity_type_names=['Calculations']

downloads = Tab('Downloads')    
downloads.calculation_sheet = Section('Updated PoFs')    
downloads.calculation_sheet.btn = DownloadButton('Download', 'download_spreadsheet')
downloads.calculation_sheet.orders = HiddenField("orders", name='orders')
downloads.calculation_sheet.types = HiddenField("types", name='types')
downloads.calculation_sheet.flocs = HiddenField("flocs", name='flocs')
downloads.calculation_sheet.fail_rates = HiddenField("rates", name='fail_rates')
downloads.calculation_sheet.failures = HiddenField("failures", name='failures')

Hi Bart, thanks. The callback function (get_color_options) should not be located within the class itself (see User input, fields and buttons - Options & selections | VIKTOR Documentation). I assume the spacings in your example are a bit messed up due to pasting, but it should look something like this:

from viktor.parametrization import Parametrization, Tab, Section, OptionField, MultiSelectField, Table
from viktor.parametrization import NumberField, LineBreak, DownloadButton, TextField, ActionButton
from viktor.parametrization import HiddenField, ChildEntityOptionField, BooleanField


def get_color_options(params, **kwargs):
    color_options = ['blue', 'green']

    if params.general.object.include_red:
        color_options.append('red')

    return color_options


class CalculationParametrization(Parametrization):
    general = Tab('General')
    general.object = Section('Object')
    general.object.type = OptionField('Pick a color', options=get_color_options)
    general.object.include_red = BooleanField('Include red')
    
    general.equipment = Section('Equipment')
    general.equipment.nl = LineBreak()
    general.equipment.child_select = ChildEntityOptionField('Select a child') #, entity_type_names=['Calculations']
    
    downloads = Tab('Downloads')    
    downloads.calculation_sheet = Section('Updated PoFs')    
    downloads.calculation_sheet.btn = DownloadButton('Download', 'download_spreadsheet')
    downloads.calculation_sheet.orders = HiddenField("orders", name='orders')
    downloads.calculation_sheet.types = HiddenField("types", name='types')
    downloads.calculation_sheet.flocs = HiddenField("flocs", name='flocs')
    downloads.calculation_sheet.fail_rates = HiddenField("rates", name='fail_rates')
    downloads.calculation_sheet.failures = HiddenField("failures", name='failures')

Hi Kevin,

Daar had ik hem ook geprobeerd, maar kreeg ik deze melding.

2022-03-29 13:13:41.458 INFO : Applying manifest…
2022-03-29 13:13:47.054 ERROR : Manifest is invalid: Entity type Calculation is wrongly defined: Invalid parametrization

Helaas …

Had je include_red ook onder de eerst Tab gezet zoals in mn voorbeeld?

general.object.include_red = BooleanField('Include red')

Klopt! Dom van mij. Ik had hem helemaal niet gedefinieerd.

Het werkt!

1 Like