Class for creating multi-select parameter widgets that allow users to choose multiple options from a list.This class can be imported from the squirrels.parameters or the squirrels module.
Factory methods are class methods that create and configure parameter instances. These methods are typically used in the pyconfigs/parameters.py file to create the parameter configurations (which describes the “shape” of the parameter but does not include the realtime user selections).
Decorator for creating a simple parameter from a function that returns select options.The decorated function must return a list of SelectParameterOption objects.
The unique identifier for this parameter. Used to reference the parameter at query time (such as in context.py or when specifying parameter selections in the APIs).
Decorator for creating a parameter with options that can vary based on user attributes or parent parameter selections.The decorated function must return a list of SelectParameterOption objects. This is functionally equivalent to create_simple but with additional arguments available for user_attribute and parent_name.
The unique identifier for this parameter. Used to reference the parameter at query time (such as in context.py or when specifying parameter selections in the APIs).
A user attribute (like “access_level”) that determines which options are visible to different users. The decorated function should return options with matching user_groups values.To use custom user fields defined in pyconfigs/user.py, prefix with custom_fields. (e.g., "custom_fields.department").
The name of a parent parameter that controls which options are visible. The decorated function should return options with parent_option_ids matching the parent’s selected value.
Decorator for creating a parameter populated from a database table or query using a SelectDataSource.The decorated function must return a SelectDataSource object.
The unique identifier for this parameter. Used to reference the parameter at query time (such as in context.py or when specifying parameter selections in the APIs).
A user attribute that determines which options from the data source are visible to different users.To use custom user fields defined in pyconfigs/user.py, prefix with custom_fields..
A sequence of custom field values, or an empty sequence if no options are selected (unless none_is_all is True, in which case all options are returned).
Returns True if more than zero options were selected. False otherwise.Note that even when this returns False, all methods starting with “get_selected” would return the full list of options if none_is_all is set to True.
This example shows different options based on user access levels.
Copy
from squirrels import parameters as p, parameter_options as po@p.MultiSelectParameter.create_with_options( name="report_sections", label="Report Sections", description="Select which sections to include in the report", user_attribute="access_level")def report_sections_options(): return [ po.SelectParameterOption( id="summary", label="Executive Summary", is_default=True, user_groups=["admin", "member", "guest"] ), po.SelectParameterOption( id="metrics", label="Key Metrics", user_groups=["admin", "member", "guest"] ), po.SelectParameterOption( id="financial", label="Financial Details", user_groups=["admin", "member"] ), ]
If custom user fields are defined in pyconfigs/user.py, then they can be used to restrict visibility of parameter options as well. To do so, the user_attribute argument must be prefixed with custom_fields..
Copy
from squirrels import parameters as p, parameter_options as po@p.MultiSelectParameter.create_with_options( name="report_sections", label="Report Sections", description="Select which sections to include in the report", user_attribute="custom_fields.role")def report_sections_options(): return [ po.SelectParameterOption( id="summary", label="Executive Summary", user_groups=["manager", "staff"] ), po.SelectParameterOption( id="financial", label="Financial Details", user_groups=["manager"] ), ]
This example populates multi-select options from a database table.
Copy
from squirrels import parameters as p, data_sources as ds@p.MultiSelectParameter.create_from_source( name="store_locations", label="Store Locations", description="Select one or more store locations")def store_locations_source() -> ds.SelectDataSource: return ds.SelectDataSource( table_or_query=""" SELECT store_id, store_name, is_flagship AS is_default FROM stores WHERE is_active = 1 ORDER BY store_name """, id_col="store_id", options_col="store_name", is_default_col="is_default" )
This example shows a multi-select that depends on a parent parameter, both populated from database sources.
Copy
from squirrels import parameters as p, data_sources as ds# First define the parent parameter@p.SingleSelectParameter.create_from_source( name="region", label="Region", description="Select a region")def region_source(): return ds.SelectDataSource( table_or_query="regions", id_col="region_id", options_col="region_name" )# Child multi-select that cascades based on region@p.MultiSelectParameter.create_from_source( name="stores_in_region", label="Stores in Region", description="Select stores within the chosen region", parent_name="region")def stores_in_region_source(): return ds.SelectDataSource( table_or_query=""" SELECT store_id, store_name, region_id FROM stores WHERE is_active = 1 ORDER BY store_name """, id_col="store_id", options_col="store_name", parent_id_col="region_id" )
Once parameters are configured, you can use instance methods in your models to access the selected values. The parameter instances are available through the context object (e.g., sqrl.prms).
To omit the WHERE clause entirely when no options are selected, you can do so as follows:
Copy
-- models/federates/filtered_products.sqlSELECT *FROM products{%- if prms["categories"].has_non_empty_selection() %}WHERE category_id IN ({{ prms["categories"].get_selected_ids() | quote_and_join }}){%- endif %}
It is generally better to only use the instance methods in context.py to transform parameter selections into context variables. Using the instance methods directly in the data models is not recommended.IDEs can provide code suggestions for the available instance methods in Python instead of having to memorize which method (such as get_selected_ids_as_list) is available to use for MultiSelectParameter objects.