Class for creating numeric range parameter widgets that allow users to select a lower and upper numeric value.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 numeric 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).
Default lower value for this parameter. Must be selectable based on min_value, max_value, and increment. Must be less than or equal to default_upper_value. If None, defaults to min_value.
Default upper value for this parameter. Must be selectable based on min_value, max_value, and increment. Must be greater than or equal to default_lower_value. If None, defaults to max_value.
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 NumberRangeParameterOption 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).
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 NumberRangeDataSource.The decorated function must return a NumberRangeDataSource 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 uses decimal values for precise range selection. Regardless of whether Decimal, float, or string values are used, the exact precision will always be maintained (without floating point errors).
Copy
from squirrels import parameters as pfrom decimal import Decimal@p.NumberRangeParameter.create_simple( name="rating_range", label="Rating Range", min_value="0.2", max_value="5.0", increment=0.2, default_lower_value=Decimal("2.4"), default_upper_value=Decimal("5.0"), description="Select rating range (0.2-5.0)")def rating_range_default(): pass
This example populates numeric range constraints from a database query.
Copy
from squirrels import parameters as p, data_sources as ds@p.NumberRangeParameter.create_from_source( name="stock_price_range", label="Stock Price Range", description="Select price range based on historical data")def stock_price_range_source() -> ds.NumberRangeDataSource: return ds.NumberRangeDataSource( table_or_query=""" SELECT MIN(price) AS min_value, MAX(price) AS max_value, PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY price) AS default_lower, PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY price) AS default_upper, 1 AS increment FROM stock_prices WHERE date >= CURRENT_DATE - INTERVAL '1 year' """, default_lower_value_col="default_lower", default_upper_value_col="default_upper", min_value_col="min_value", max_value_col="max_value", increment_col="increment" )
This example shows a number 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 departments@p.SingleSelectParameter.create_from_source( name="department", label="Department", description="Select a department")def department_source(): return ds.SelectDataSource( table_or_query="departments", id_col="department_id", options_col="department_name" )@p.NumberRangeParameter.create_from_source( name="salary_range", label="Salary Range", description="Select salary range for the department", parent_name="department")def salary_range_source(): return ds.NumberRangeDataSource( table_or_query=""" SELECT department_id, MIN(salary) AS min_value, MAX(salary) AS max_value, AVG(salary) - STDDEV(salary) AS default_lower, AVG(salary) + STDDEV(salary) AS default_upper, 1000 AS increment FROM employees GROUP BY department_id """, default_lower_value_col="default_lower", default_upper_value_col="default_upper", min_value_col="min_value", max_value_col="max_value", increment_col="increment", parent_id_col="department_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_lower_value) is available to use for NumberRangeParameter objects.