Skip to main content
Data source class for populating text parameter options from a database table or query. This class can be imported from the squirrels.data_sources or the squirrels module.

Constructor

Creates a TextDataSource object.
def __init__(
    self, table_or_query: str, default_text_col: str, 
    *, id_col: str | None = None, source: SourceEnum = SourceEnum.CONNECTION, 
    user_group_col: str | None = None, parent_id_col: str | None = None, 
    connection: str | None = None
) -> None:

Examples

A TextDataSource object is created in the pyconfigs/parameters.py file. It must be created in a function decorated with the create_from_source factory method from TextParameter.

Usage example in parameters.py

from squirrels import parameters as p, data_sources as ds

@p.TextParameter.create_from_source(
    name="search_query", 
    label="Search Query",
    description="Enter search terms"
)
def search_query_source():
    return ds.TextDataSource(
        table_or_query="""
            SELECT 'product' AS default_search
        """,
        default_text_col="default_search"
    )
In addition, the following are some additional examples for creating a TextDataSource object.

Using a table from a specific connection

This example uses a table called “default_values” from the “config_db” connection.
ds.TextDataSource(
    table_or_query="default_values",
    default_text_col="search_term",
    connection="config_db"
)
The connection must either be defined in squirrels.yml or the connections.py file.

Using seeds as the data source

This example uses a seed file called “text_defaults”.
ds.TextDataSource(
    table_or_query="text_defaults",
    default_text_col="default_value",
    source=ds.SourceEnum.SEEDS
)

Enabling cascading effects with a parent parameter

In this example, the default text value is determined by the selected value of another parameter called “template_type”.
@p.TextParameter.create_from_source(
    name="message_template", 
    label="Message Template",
    description="The default message template for the selected type",
    parent_name="template_type"
)
def message_template_source():
    return ds.TextDataSource(
        table_or_query="""
            SELECT 
                template_type_id,
                default_message
            FROM message_templates
        """,
        default_text_col="default_message",
        parent_id_col="template_type_id"  # Cascades based on template_type selection
    )