Class for creating single-select dropdown parameter widgets that allow users to choose one option 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..
If field does not exist for the selected option and default_field is None, returns this default value. Ignored if field is None or default_field is not None.
This example restricts certain options based on user access levels.
Copy
from squirrels import parameters as p, parameter_options as po@p.SingleSelectParameter.create_with_options( name="report_type", label="Report Type", description="Type of report to generate", user_attribute="access_level")def report_type_options(): return [ po.SelectParameterOption( id="basic_report", label="Basic Report", is_default=True, user_groups=["admin", "member", "guest"] ), po.SelectParameterOption( id="detailed_report", label="Detailed Report", user_groups=["admin", "member"] ), po.SelectParameterOption( id="financial_report", label="Financial Report (Admin Only)", user_groups=["admin"] ), ]
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.SingleSelectParameter.create_with_options( name="report_type", label="Report Type", description="Type of report to generate", user_attribute="custom_fields.role")def report_type_options(): return [ po.SelectParameterOption( id="basic_report", label="Basic Report", user_groups=["manager", "staff"] ), po.SelectParameterOption( id="detailed_report", label="Detailed Report", user_groups=["manager"] ), ]
This example shows cascading from a database where products depend on the selected category.
Copy
from squirrels import parameters as p, data_sources as ds@p.SingleSelectParameter.create_from_source( name="product", label="Product", description="Select a product from the chosen category", parent_name="product_category")def product_source(): return ds.SelectDataSource( table_or_query=""" SELECT product_id, product_name, category_id FROM products WHERE is_active = 1 ORDER BY product_name """, id_col="product_id", options_col="product_name", parent_id_col="category_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).
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_id) is available to use for SingleSelectParameter objects.