pyconfigs/parameters.py file allows you to define parameter widgets for your Squirrels project using Python. Parameters are interactive widgets that let users (and AI) customize dataset results at query time.
Parameters can also be defined in the squirrels.yml file. However, defining parameters here in Python is strongly recommended because:
- IDEs provide autocomplete and type checking for Python
- You get better error messages during development
- Python allows more complex logic and dynamic options
File structure
Theparameters.py file defines parameters using decorator functions. The decorator usually specifies the parameter configurations, and the decorated function returns the parameter options (if applicable).
pyconfigs/parameters.py
The ParametersArgs object
The decorated function for parameter options can optionally define asqrl argument. The sqrl argument is a ParametersArgs object that provides useful properties for building parameters options dynamically.
| Property | Type | Description |
|---|---|---|
project_path | str | Absolute path to the Squirrels project directory |
proj_vars | dict[str, Any] | Project variables from squirrels.yml |
env_vars | dict[str, str] | Environment variables from .env files and system |
Parameter types
Squirrels supports seven parameter types, each with specific factory methods for creation:| Type | Description | Widget |
|---|---|---|
| SingleSelectParameter | Single option from a dropdown | Dropdown |
| MultiSelectParameter | Multiple options from a list | Multi-select |
| DateParameter | Single date selection | Date picker |
| DateRangeParameter | Start and end date range | Date range picker |
| NumberParameter | Single numeric value | Number input/slider |
| NumberRangeParameter | Lower and upper numeric bounds | Range slider |
| TextParameter | Free-form text input | Text field |
Factory methods
Each parameter type has factory methods (decorators) for creating parameters. The three common patterns are:| Factory Method | Description | Returns |
|---|---|---|
create_simple() | Static options for dropdown / multi-select, otherwise does nothing for other parameter types | List of parameter options or None |
create_with_options() | Parameter options with user attributes or parent cascading | List of parameter options |
create_from_source() | Parameter options populated from a database or seed | Data source object |
Parent-child relationships
Parameters can be linked in parent-child relationships where the child parameter’s available options depend on the parent parameter’s selection. This is configured as follows based on the factory method used:create_with_options(): Use theparent_nameargument in the decorator andparent_option_idsin each parameter optioncreate_from_source(): Use theparent_nameargument in the decorator andparent_id_colin the data source object
Rules for parent parameters
- The parent parameter must be a select parameter (SingleSelectParameter or MultiSelectParameter)
- If the child parameter is not a select parameter (e.g., DateParameter, NumberParameter, etc.), then the parent must be a SingleSelectParameter
Rules for child parameter options
When usingcreate_with_options(), the rules for parent_option_ids depend on whether the child is a select parameter:
| Child parameter type | Rule for parent_option_ids |
|---|---|
| Select parameter | Each parent option id may appear in zero or more child options |
| Non-select parameter | Each parent option id can appear in at most one child option |
create_from_source(), these rules are enforced based on the parent_id_col values in the data source.
The same rules apply to
user_groups (for create_with_options) and user_group_col (for create_from_source) when using user_attribute to filter options by a user attribute.Disabled parameters
A parameter becomes disabled when it has no available options. This can happen when:- The parent parameter’s selected value does not match any
parent_option_idsin the child’s options - The user’s attribute value does not match any
user_groupsin the parameter’s options
sqrl.param_exists("parameter_name") returns False in context.py and data models.
Examples
Simple single-select parameter
Create a dropdown where users select one option:pyconfigs/parameters.py
Multi-select parameter with user-specific options
Create a multi-select that shows different options based on user access level:pyconfigs/parameters.py
pyconfigs/user.py to restrict visibility of parameter options.
pyconfigs/parameters.py
Single-select from database query
Populate dropdown options from a database table:pyconfigs/parameters.py
Simple date parameter
Create a date parameter using the create_simple() decorator:pyconfigs/parameters.py
pass).
Date parameter as child parameter
Create a date parameter whose constraints change based on a parent parameter selection:pyconfigs/parameters.py
Single-select with custom fields
Add custom fields to options for use in data models:pyconfigs/parameters.py
columns and aliases can be accessed in context.py or data models using the get_selected() method on the parameter instance.
Using parameters in models
Once parameters are defined, they are available in your data models through the context. See the context.py documentation for details on accessing parameter selections. Example usage incontext.py:
pyconfigs/context.py
Related pages
- ParametersArgs - Properties available in the
sqrlobject argument - SingleSelectParameter - Single-select dropdown parameter
- MultiSelectParameter - Multi-select parameter
- DateParameter - Date picker parameter
- DateRangeParameter - Date range parameter
- NumberParameter - Number input parameter
- NumberRangeParameter - Number range parameter
- TextParameter - Text input parameter
- squirrels.yml parameters - YAML-based parameter configuration