Step 1: Complete the quickstart
Create a new folder for your project, and open it up in your favourite coding editor (such as VS Code). The name of the folder is your choice (one example issquirrels-tutorial).
Follow instructions in Quickstart to quickly get a sample working project going.
Then, in the .env file, set the SQRL_SECRET__ADMIN_PASSWORD environment variable to something of your choice (it is randomly generated by default).
Step 2: Add the weather database
Now add the SQLite database we will use for the rest of the tutorial. Simply run:weather.db file in the resources folder. Feel free to delete the other files in the resources folder.
Note that this is mainly done for tutorial purposes. For most production use cases, you would simply specify the database connection string (more details on this soon) to an external database and not bring a copy of the database into your project.
Step 3: Define project configurations
Open thesquirrels.yml file. This is the project configuration file that exists for all Squirrels projects, and it is used to configure many properties of the Squirrels project in yaml.
In this step, we will focus on the project_variables, connections, and datasets sections.
Setting the project variables
The project variables name and major_version are required. All other project variables are optional. You are also free to add any of your own project variables here. In this tutorial, we will be making datasets for historical weather data. Change the project_variables section to look like the following:Setting the database connections
The connections section is where we set all the database connection details that we need. We provide a list of connection names here and refer to them in other files. The connection namedefault must be provided for models that don’t specify a connection name explicitly.
Under default, change the uri field to sqlite:///{project_path}/resources/weather.db. Change the label to “SQLite Weather Database”.
You can also substitute environment variables defined in the
.env file using Jinja. For instance, if there is an environment variable called “SQLITE_CONN_STR” in .env, then you can also set the url to:sqlalchemy is the utility used to connect to any database that SQLAlchemy supports. For more information on the different connection types, see the Connections page.
To specify database connections with Python instead of YAML, see the database connections file page.
Defining the datasets
The datasets section is where we define the attributes for all datasets created by the Squirrels project. Every dataset defined will have their own “parameters API” and “dataset result API”. Currently, you may see two datasets configured. We will only have one dataset for this tutorial. Change the datasets section to look like the following instead:Step 4: Create the dataset parameters
Go into thepyconfigs/parameters.py file. This python configuration file contains the definitions of all the widget parameters used in the dataset.
We will rewrite this file. Remove all the existing code in the file and replace it with the following:
pyconfigs/parameters.py
@p.SingleSelectParameter.create_simple to create a single-select parameter. The function returns a list of parameter options as SelectParameterOption objects.
The id and label parameters to the SelectParameterOption constructor are required. Arbitrary keyword arguments such as “dim_col” and “order_by_col” can be specified to the SelectParameterOption constructor, which will be treated as custom fields to the parameter option.
The SelectParameterOption class has an “is_default” attribute to specify the parameter option(s) that are selected by default. By default, “is_default” is set to False. When none of the parameter options have “is_default” set as True, the first option is selected by default for single-select parameters, and nothing is selected by default for multi-select parameters.
The possible widget parameter types supported today are SingleSelectParameter, MultiSelectParameter, DateParameter, DateRangeParameter, NumberParameter, NumberRangeParameter, and TextParameter. Each parameter type can be created with one of the following decorators: create_simple, create_with_options, or create_from_source. Every decorator takes “name” and “label” as required arguments.For SingleSelectParameter, the arguments for create_simple and create_with_options are similar. The difference is that create_with_options lets you specify a parent parameter for cascading the shown options. For non-select parameter types like DateParameter, there are more differences.
Step 5: Create the context file
The context file is a Python file that runs in real-time to transform parameter selections and/or authenticated user attributes into meaningful values that can be used by dynamic data models. Change thepyconfigs/context.py file to look like the following:
pyconfigs/context.py
The available methods on the parameter object depends on the parameter type. For example, SingleSelectParameter objects have a
get_selected method to get a custom field from the selected option.By casting the parameter object to type SingleSelectParameter, this makes it easier to explore the available methods on the parameter through an IDE’s linting and auto-complete features.Step 6: Create sources
Themodels/sources.yml file lets us document the metadata of sources from our database tables.
The weather.db SQLite database we retrieved earlier contains a table called “weather”. Replace the sources.yml file with the following contents:
models/sources.yml
Step 7: Create seeds
Seeds are CSV data files that can be used by other data models. For example, we can create a seed that maps month numbers to month names. Currently, there are some seeds already defined in theseeds/ folder. We will replace them with our own seeds. Feel free to delete the existing files in the seeds/ folder.
Create a file named seed_month_names.csv in the seeds/ folder with the following contents.
seeds/seed_month_names.csv
seeds/seed_month_names.yml file to add metadata for the seed.
seeds/seed_month_names.yml
- Explicit type casting: By setting
cast_column_types: true(not shown here), the framework will cast the columns to the specified types. - Documentation: It serves as a central place to document the purpose of the seed and its individual columns.
- Metadata for the API: The
descriptionandcategory(dimension, measure, etc.) provide context for the data. If surfaced to a dataset, this metadata is exposed via API endpoints, allowing frontend applications to understand the purpose of each column.
Step 8: Create data models from SQL queries
For data models that are created by code, the Squirrels framework supports:- Creating build models (tables/views to be built offline) from SQL (DuckDB dialect) or Python files
- Creating dbview models (queries that run on an external database in real-time) from SQL files (dialect of the database connection used)
- Creating federate models (queries that run in the API server in real-time) from SQL (DuckDB dialect) or Python files
sources.yml, data models that are downstream of the replaced source model will no longer work.
Delete the existing files in the models/builds/, models/dbviews/, and models/federates/ folders. Feel free to delete the existing files in the macros/ folder as well.
Define a macro
First, create amacros/metrics.sql file with the following contents.
macros/metrics.sql
Define the build model
“Build models” are defined in themodels/builds/ folder.
Create a models/builds/weather_by_date.sql file with the following contents.
models/builds/weather_by_date.sql
The SQL file is templated with Jinja. It calls the macros:
get_metrics()which is defined in themacros/metrics.sqlfile we created earlierref("src_weather")which references the “src_weather” source we defined earlier
ref on sources (that have load_to_vdl: true), seeds, and other build models.models/builds/weather_by_date.yml file.
models/builds/weather_by_date.yml
Define the federate model
Federate models are defined in themodels/federates/ folder.
Create a models/federates/weather_by_period.sql file with the following contents.
models/federates/weather_by_period.sql
The
{{ ctx.dim_col }} and {{ ctx.order_col }} variables are used to reference the context variables defined in the context.py file.Just like the build model, we use the get_metrics macro again. We also use the ref macro to reference the build model created earlier.Federate models are able to call the ref macro on sources (that have load_to_vdl: true), seeds, build models, dbview models, and other federate models.models/federates/weather_by_period.yml file to add metadata for the federate model.
models/federates/weather_by_period.yml
For SQL models that use the
ref macro, the depends_on field is optional. However, it is required for Python models, and it is recommended to specify it for SQL models as well.Similar to federate models, dbview models are also run in real-time and can change behaviour based on parameter selections or authenticated user.The main difference is that dbview models run on an external database (instead of the API server), and can only reference sources that share the same database connection.
Step 9: Development testing
You can build the static data models by running:What’s next?
Now that you’ve built your first Squirrels project, here are some suggested pages to explore next:- Architecture: Learn more about the lifecycle of a Squirrels application and how data flows through the system.
- Environment variables: See the available environment variables Squirrels can use to adjust certain settings.
- MCP server: Understand the available MCP tools and their behavior for use with AI agents.
- Virtual Data Lake (VDL): Understand how the VDL works. Use it to materialize data models in advance if needed for faster real-time queries.
- Dashboards: Learn how to create custom dashboards to visualize your datasets.
- Authentication: Secure your APIs and implement user-based filtering (or other user-specific behaviours).
- Cascading parameter options: Enable parent-child relationships between parameters where a child parameter’s options depend on a parent selection.
- CLI reference: A complete guide to all Squirrels CLI commands.