Class for creating date range picker parameter widgets that allow users to select a start date and an end date.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 date range parameter that doesn’t involve user attributes or parent parameters.The body of the decorated function does not need to return anything (i.e., it can simply be pass).
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 DateRangeParameterOption 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 DateRangeDataSource.The decorated function must return a DateRangeDataSource 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..
This example populates date range constraints from a database query.
Copy
from squirrels import parameters as p, data_sources as ds@p.DateRangeParameter.create_from_source( name="available_data_range", label="Available Data Range", description="Select date range from available data")def available_data_range_source() -> ds.DateRangeDataSource: return ds.DateRangeDataSource( table_or_query=""" SELECT MIN(transaction_date) AS min_date, MAX(transaction_date) AS max_date, MAX(transaction_date) - INTERVAL '30 days' AS default_start, MAX(transaction_date) AS default_end FROM transactions """, default_start_date_col="default_start", default_end_date_col="default_end", min_date_col="min_date", max_date_col="max_date" )
This example shows a date range parameter whose constraints come from a database and depend on a parent parameter.
Copy
from squirrels import parameters as p, data_sources as ds# Parent parameter for projects@p.SingleSelectParameter.create_from_source( name="project", label="Project", description="Select a project")def project_source(): return ds.SelectDataSource( table_or_query="projects", id_col="project_id", options_col="project_name" )# Child date range parameter with constraints from database@p.DateRangeParameter.create_from_source( name="project_milestone_date", label="Milestone Date", description="Select a milestone date for the project", parent_name="project")def project_milestone_date_source(): return ds.DateRangeDataSource( table_or_query=""" SELECT project_id, project_start_date AS min_date, project_end_date AS max_date, project_start_date AS default_start, project_end_date AS default_end FROM projects """, default_start_date_col="default_start", default_end_date_col="default_end", min_date_col="min_date", max_date_col="max_date", parent_id_col="project_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).
The following example works but is not recommended. See tip below for why.
Copy
-- models/federates/daily_report.sqlSELECT *FROM salesWHERE sale_date BETWEEN ( {{ prms["analysis_window"].get_selected_start_date() | quote }} AND {{ prms["analysis_window"].get_selected_end_date() | quote }})
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_start_date and get_selected_end_date) is available to use for DateRangeParameter objects.