Class for creating date picker parameter widgets that allow users to select a single 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 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 DateParameterOption 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 DateDataSource.The decorated function must return a DateDataSource 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 provides different default dates based on user groups.
Copy
from squirrels import parameters as p, parameter_options as pofrom datetime import date, timedelta@p.DateParameter.create_with_options( name="access_date", label="Access Date", description="Select date to view data", user_attribute="access_level")def access_date_options(): today = date.today() return [ po.DateParameterOption( default_date=today, min_date=today - timedelta(days=7), user_groups=["guest"] # Guests can only access last 7 days ), po.DateParameterOption( default_date=today, min_date=today - timedelta(days=90), user_groups=["member"] # Members can access last 90 days ), po.DateParameterOption( default_date=today, min_date=date(2020, 1, 1), user_groups=["admin"] # Admins have full access ), ]
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 pofrom datetime import date, timedelta@p.DateParameter.create_with_options( name="access_date", label="Access Date", description="Select date to view data", user_attribute="custom_fields.department")def access_date_options(): today = date.today() return [ po.DateParameterOption( default_date=today, user_groups=["sales"] ), po.DateParameterOption( default_date=today - timedelta(days=30), min_date=date(2020, 1, 1), user_groups=["engineering"] ), ]
This example populates date constraints from a database query.
Copy
from squirrels import parameters as p, data_sources as ds@p.DateParameter.create_from_source( name="data_date", label="Data Date", description="Select a date with available data")def data_date_source() -> ds.DateDataSource: return ds.DateDataSource( table_or_query=""" SELECT MAX(report_date) AS default_date, MIN(report_date) AS min_date, MAX(report_date) AS max_date FROM daily_reports """, default_date_col="default_date", min_date_col="min_date", max_date_col="max_date" )
This example shows a date 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 parameter with constraints from database@p.DateParameter.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.DateDataSource( table_or_query=""" SELECT project_id, project_start_date AS min_date, project_end_date AS max_date, CURRENT_DATE AS default_date FROM projects """, default_date_col="default_date", 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).
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_date) is available to use for DateParameter objects.