MultiSelectParameter (class)
MultiSelectParameter objects can be used to interact with real-time selections for multi-select parameters. It also contains factory methods as static / class methods for creating multi-select parameters. This is a subclass of Parameter.
The class can be imported from the squirrels.parameters
or squirrels
module.
Static / Class Methods
CreateSimple, CreateWithOptions and CreateFromSource are factory methods for creating multi-select widget parameters in the Squirrels application.
CreateSimple
@classmethod
def CreateSimple(
cls, name: str, label: str, all_options: typing.Sequence[squirrels.SelectParameterOption | dict], *,
description: str = "", show_select_all: bool = True, order_matters: bool = False,
none_is_all: bool = True
) -> None:
Creates the configurations for a multi-select parameter by providing a list of parameter option objects, and adds it to a pool of parameter configurations that datasets can pick from.
Required Arguments:
- name: A string for the name of the parameter
- label: A string for human-friendly display name for this parameter
- all_options: A sequence of SelectParameterOption objects (or serialization of the objects as dictionaries) for all parameter options associated to this parameter
Optional Keyword Arguments:
- description: A string for the explanation / purpose of the parameter. Default is empty string
- show_select_all: A boolean for whether this parameter should have a checkbox/button to automatically select all options. Default is True
- order_matters: A boolean for whether the ordering of the selection matters. Default is False
- none_is_all: A boolean for whether applying no selection is equivalent to selecting all. Default is True
Returns: None
CreateWithOptions
@classmethod
def CreateWithOptions(
cls, name: str, label: str, all_options: typing.Sequence[squirrels.SelectParameterOption | dict], *,
description: str = "", show_select_all: bool = True, order_matters: bool = False,
none_is_all: bool = True, user_attribute: str | None = None, parent_name: str | None = None
) -> None:
Creates the configurations for a multi-select parameter by providing a list of the parameter option objects, and adds it to a pool of parameter configurations that datasets can pick from.
Required Arguments:
- name: A string for the name of the parameter
- label: A string for human-friendly display name for this parameter
- all_options: A sequence of SelectParameterOption objects (or serialization of the objects as dictionaries) for all parameter options associated to this parameter
Optional Keyword Arguments:
- description: A string for the explanation / purpose of the parameter. Default is empty string
- show_select_all: A boolean for whether this parameter should have a checkbox/button to automatically select all options. Default is True
- order_matters: A boolean for whether the ordering of the selection matters. Default is False
- none_is_all: A boolean for whether applying no selection is equivalent to selecting all. Default is True
- user_attribute: An optional string for the user attribute that may cascade the options for this parameter. If None, then the authorized user has no effect on the selectable parameter options. Default is None
- parent_name: An optional string for the name of the parent parameter that may cascade the options for this parameter. If None, then other parameters have no effect on the selectable parameter options for this parameter. Default is None
Returns: None
CreateFromSource
@classmethod
def CreateFromSource(
cls, name: str, label: str, data_source: squirrels.SelectDataSource | dict, *,
description: str = "", show_select_all: bool = True, order_matters: bool = False,
none_is_all: bool = True, user_attribute: str | None = None, parent_name: str | None = None
) -> None:
Creates the configurations for a multi-select parameter by providing a lookup table to query from, and adds it to a pool of parameter configurations that datasets can pick from.
Required Arguments:
- name: A string for the name of the parameter
- label: A string for human-friendly display name for this parameter
- data_source: A SelectDataSource object (or serialization of the object as a dictionary) which contains details of the lookup table from the external database
Optional Keyword Arguments:
Same optional arguments as the CreateWithOptions static method described above.
Returns: None
Non-Static Methods
In the context.py file or data model, the methods below can be invoked on a MultiSelectParameter object to retrieve details for the selected parameter option at runtime. For example, the following code demonstrates getting the MultiSelectParameter object in context.py
, and calling the "get_selected_list" method on it.
if sqrl.param_exists("my_ms_param"):
my_ms_param = prms["my_ms_param"]
assert isinstance(my_ms_param, p.MultiSelectParameter)
my_special_field: list[str] = my_ms_param.get_selected_list("some_field")
...
has_non_empty_selection
def has_non_empty_selection(self) -> bool:
Gets whether there are options selected. True if more than zero options were selected, and False otherwise.
Returns: A boolean.
get_selected_list
def get_selected_list(
self, field: str | None = None, *,
default_field: str | None = None, default: typing.Any = None
) -> typing.Sequence[squirrels.SelectParameterOption | typing.Any]:
Gets the selected list of multi-select options or custom field values.
Optional Arguments:
- field: An optional string for the "custom_fields" attribute to retrieve from the selected option. If None, retrieves the selected SelectParameterOption instance instead. Default is None
Optional Keyword Arguments:
- default_field: An optional string. If not None, this is used if the "field" argument cannot be found in "custom_fields". Default is None
- default: A optional value (of any type) to return if the "field" and "default_field" arguments cannot be found in "custom_fields". If None, an error is thrown if "field" and "default_field" arguments cannot be found. Default is None
Returns: A sequence of the custom field values (of any type) or a sequence of SelectParameterOption instances if the "field" argument is None.
get_selected_ids_as_list
def get_selected_ids_as_list(self) -> typing.Sequence[str]:
Gets the sequence of ID(s) of the selected option(s).
Returns: A sequence of strings for the IDs.
get_selected_ids_joined
def get_selected_ids_joined(self) -> str:
Gets the ID(s) of the selected option(s) joined by comma.
Returns: A string containing all the IDs separated by comma.
get_selected_ids_quoted_as_list
def get_selected_ids_quoted_as_list(self) -> typing.Sequence[str]:
Gets the sequence of ID(s) of the selected option(s) surrounded by single quotes.
Returns: A sequence of strings for the quoted IDs.
get_selected_ids_quoted_joined
def get_selected_ids_quoted_joined(self) -> str:
Gets the ID(s) of the selected option(s) surrounded by single quotes and joined by comma.
Returns: A string containing all the quoted IDs separated by comma.
get_selected_labels_as_list
def get_selected_labels_as_list(self) -> typing.Sequence[str]:
Gets the sequence of label(s) of the selected option(s).
Returns: A sequence of strings for the labels.
get_selected_labels_joined
def get_selected_labels_joined(self) -> str:
Gets the label(s) of the selected option(s) joined by comma.
Returns: A string containing all the labels separated by comma.
get_selected_labels_quoted_as_list
def get_selected_labels_quoted_as_list(self) -> typing.Sequence[str]:
Gets the sequence of label(s) of the selected option(s) surrounded by single quotes.
Returns: A sequence of strings for the quoted labels.
get_selected_labels_quoted_joined
def get_selected_labels_quoted_joined(self) -> str:
Gets the label(s) of the selected option(s) surrounded by single quotes and joined by comma.
Returns: A string containing all the quoted labels separated by comma.