Skip to main content
The 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

The parameters.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 a sqrl argument. The sqrl argument is a ParametersArgs object that provides useful properties for building parameters options dynamically.

Parameter types

Squirrels supports seven parameter types, each with specific factory methods for creation:

Factory methods

Each parameter type has factory methods (decorators) for creating parameters. The three common patterns are:
Use create_simple() when you don’t need user-specific options or parent-child cascading. Use create_with_options() when you need either feature. Use create_from_source() when options should come from a database table or seed file.

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 the parent_name argument in the decorator and parent_option_ids in each parameter option
  • create_from_source(): Use the parent_name argument in the decorator and parent_id_col in the data source object

Rules for parent parameters

Rules for child parameter options

When using create_with_options(), the rules for parent_option_ids depend on whether the child is a select parameter: When using 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_ids in the child’s options
  • The user’s attribute value does not match any user_groups in the parameter’s options
When a parameter is disabled, 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
You can also choose to use custom user fields defined in 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
With the exception of SingleSelectParameter and MultiSelectParameter, functions decorated with the create_simple() decorator should not return anything (i.e., it can simply be pass).

Date parameter as child parameter

Create a date parameter whose constraints change based on a parent parameter selection:
pyconfigs/parameters.py
When the user selects “FY 2023”, the date picker will be constrained to dates in 2023. When “FY 2024” is selected, the constraints switch to 2024.

Single-select with custom fields

Add custom fields to options for use in data models:
pyconfigs/parameters.py
Custom fields like 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 in context.py:
pyconfigs/context.py