pyconfigs/context.py file allows you to define context variables that are shared across all your data models. These variables are computed after parameter selections are made and are available to both Jinja SQL templates and Python data models.
The
main function in context.py runs every time an API request is made, after parameter selections are processed but before data models are executed. This makes it the ideal place to transform parameter selections into reusable context variables.File structure
Thecontext.py file must define a main function with the following signature:
pyconfigs/context.py
The ContextArgs object
Thesqrl argument is a ContextArgs object that provides useful properties and methods for building context variables.
Common methods
param_exists()
Checks whether a parameter exists and is enabled (has available options).True if the parameter exists and is enabled, False otherwise. A parameter becomes disabled when it has no available options (e.g., due to parent-child cascading or user attribute filtering).
set_placeholder()
Sets a placeholder value for use in SQL queries. This is useful for dynamic SQL generation where you want to use placeholders instead of direct string interpolation.Accessing parameters
To access parameter selections incontext.py, use sqrl.param_exists() to check if a parameter exists, then access it from sqrl.prms. Each parameter type has specific methods to get selected values:
pyconfigs/context.py
Examples
Single-select parameter
Extract the selected ID from a single-select parameter:pyconfigs/context.py
Multi-select parameter
Extract selected IDs as a list and check if any selections were made:pyconfigs/context.py
Date parameters
Extract date values from date and date range parameters:pyconfigs/context.py
Number parameters
Extract numeric values from number and number range parameters:pyconfigs/context.py
Text parameter
Set a placeholder for text input value (recommended for SQL injection prevention):pyconfigs/context.py
Accessing custom fields from parameter options
If your parameter options have custom fields, you can access them usingget_selected():
pyconfigs/context.py
User-based context variables
Access user information to create user-specific context variables:pyconfigs/context.py
Using context variables in models
In Jinja SQL templates
Access context variables using{{ ctx.variable_name }}:
models/federates/fed_sales_report.sql
In Python models
Access context variables usingsqrl.ctx["variable_name"]:
models/federates/fed_sales_report.py
Using functions from context
You can store functions in the context dictionary and use them in Python models:pyconfigs/context.py
models/federates/fed_model.py
Placeholders
Placeholders are a way to set values that can be referenced in SQL queries using parameterized queries (which helps prevent SQL injection). Usesqrl.set_placeholder() to set placeholder values:
pyconfigs/context.py
:placeholder_name (for SQLAlchemy) or $placeholder_name (for DuckDB):
models/federates/fed_model.sql
Best practices
-
Check parameter existence: Always use
sqrl.param_exists()before accessing parameters to avoid errors when parameters are disabled or don’t exist. -
Type assertions: Use
isinstance()checks to ensure you’re working with the correct parameter type before calling type-specific methods. -
Centralize logic: Put parameter transformation logic in
context.pyrather than accessing parameters directly in data models. - Use descriptive names: Choose clear, descriptive names for context variables that indicate their purpose.
-
Handle missing values: Use
.get()with defaults when accessing context variables in Python models to handle cases where variables might not be set.
Related pages
- ContextArgs - Full reference for the
sqrlargument object - parameters.py - How to define parameters
- user.py - How to define custom user fields
- SingleSelectParameter - Single-select parameter reference
- MultiSelectParameter - Multi-select parameter reference
- DateParameter - Date parameter reference
- DateRangeParameter - Date range parameter reference
- NumberParameter - Number parameter reference
- NumberRangeParameter - Number range parameter reference
- TextParameter - Text parameter reference