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

The context.py file must define a main function with the following signature:
pyconfigs/context.py

The ContextArgs object

The sqrl 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).
Returns 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.
Returns an empty string (so it can be called from Jinja templates). See the placeholders section for more details.

Accessing parameters

To access parameter selections in context.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
It’s generally better to transform parameter selections into context variables in context.py rather than accessing parameters directly in data models. This provides:
  • Better IDE autocomplete and type checking
  • Centralized logic for parameter transformations
  • Easier maintenance and testing

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 using get_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 using sqrl.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). Use sqrl.set_placeholder() to set placeholder values:
pyconfigs/context.py
In SQL templates, you can then reference placeholders using :placeholder_name (for SQLAlchemy) or $placeholder_name (for DuckDB):
models/federates/fed_model.sql
When using placeholders, ensure your connection type supports them. SQLAlchemy connections support :param_name syntax, while DuckDB connections support $param_name syntax. ConnectorX and ADBC connections do not support placeholders.

Best practices

  1. Check parameter existence: Always use sqrl.param_exists() before accessing parameters to avoid errors when parameters are disabled or don’t exist.
  2. Type assertions: Use isinstance() checks to ensure you’re working with the correct parameter type before calling type-specific methods.
  3. Centralize logic: Put parameter transformation logic in context.py rather than accessing parameters directly in data models.
  4. Use descriptive names: Choose clear, descriptive names for context variables that indicate their purpose.
  5. Handle missing values: Use .get() with defaults when accessing context variables in Python models to handle cases where variables might not be set.