Dynamically create parameters


How can I dynamically create parameters based on previous user input?
For example, if parametera options are ‘single’ and ‘double’,
I would like to generate one new parameter if the user chooses single and two new parameters if the user chooses double.
I tried to use a call back function and then an if statement but it didn’t work.

Thanks for your help!

def get_parametera_options(params, **kwargs):
    if params.parametera == 'Single':
        return ['1']
    if params.parametera == 'Double':
        return ['2']
class Parametrization(ViktorParametrization): 
    parametera = OptionField('Type', options=['Single', 'Double'])
    # if user chooses Single, I want to generate parameter b
    # if user chooses Double, I want to generate parameter c and d
    # but I don't need parameter d to show if it is single
    # Calling get_parametera_options doesn't work 

Hi Melissa,

Welcome on the forum!

You can dynamically set the visibility of a field to achieve this:


Hi, thank you for your reply.

It seems that in the provided example, the parameter is hidden depending of the boolean value. In my case, I would like to hide one or more parameters if the value is ‘single’ (because actually I also have a triple option so it is not just boolean).

Is there a way to have it visible base on the string value selected by the user?

Thank you

Hi Melissa,

The function provided in the example can be altered as you like. For example, you may assert string values like this:

def param_y_visible(params, **kwargs):
    return params.param_x != "Single"  # returns False if param_x is "Single"

Hi, it works great!
Thank you