provides the `%%dbt` cell magic for creating models and analyses and `%dbtconfig` to configure the _dbt project dir_ and _dbt profiles dir_

clear_cache[source]

clear_cache(project_dir=None)

Removes all existing nbdbt cache entries

config_dbt[source]

config_dbt(line)

::

%config_dbt [-d PROFILE] [-p PROJECT] [-n NOTEBOOK] [-l LIMIT]

optional arguments: -d PROFILE, --profile PROFILE If provided, override the dbt profiles directory (default: '~/.dbt') -p PROJECT, --project PROJECT Set the dbt project directory -n NOTEBOOK, --notebook NOTEBOOK Set the notebook path -l LIMIT, --limit LIMIT Set the default sql row limit

class DbtMagicObject[source]

DbtMagicObject(raw_sql:str, file:str, limit:int, project_dir:Union[str, Path, NoneType]=None, notebook_name:Optional[str]=None, profile_dir:Union[str, Path, NoneType]=None)

Type Default Details
raw_sql str sql string
file str path to sql file (relative to dbt project dir)
limit int limit row default
project_dir typing.Union[str, pathlib.Path, NoneType] None dbt project dir
notebook_name typing.Union[str, NoneType] None name of notebook
profile_dir typing.Union[str, pathlib.Path, NoneType] None dbt profiles dir

DbtMagicObject.ref[source]

DbtMagicObject.ref(limit=-1)

None[source]

write_dbt[source]

write_dbt(line, cell)

::

%write_dbt [-a ASSIGN] [-p PROJECT] [-n NOTEBOOK] [-l LIMIT] file

positional arguments: file file path to write to

optional arguments: -a ASSIGN, --assign ASSIGN If provided, save the output to this variable instead of displaying it. -p PROJECT, --project PROJECT dbt project directory -n NOTEBOOK, --notebook NOTEBOOK notebook source file -l LIMIT, --limit LIMIT sql limit default

%dbtconfig -p ../my_dbt_project -n notebooks/00_dbt_cellmagic.ipynb
%%dbt analyses/sample1.sql

select *
from {{ ref('my_first_dbt_model') }}
where id is not null
id
0 1
if "sample2" in globals():
    del sample2
%%dbt -a sample2 analyses/sample2.sql 

select *
from {{ ref('my_second_dbt_model') }}
where id is not null
results = sample2.ref()
results
id
0 1
%%dbt -a my_third_model -n notebooks/index.ipynb models/my_third_model.sql
select *
from {{ ref('my_second_dbt_model') }}
my_third_model
<__main__.DbtMagicObject at 0x7efe6b4cb190>

The ref method on DbtMagicObject allows us to run the query and save the results into a dataframe.

results = my_third_model.ref()
results  # dataframe
id
0 1
faldbt = FalDbt(nbdbt_config["project_dir"], nbdbt_config["profiles_dir"])
model0 = faldbt.list_models()[0]
source0 = faldbt.list_sources()[0]
model0.schema
table_catalog table_schema table_name column_name ordinal_position is_nullable data_type is_generated generation_expression is_stored is_hidden is_updatable is_system_defined is_partitioning_column clustering_ordinal_position collation_name
0 sample-dbt-learn-project jaffle_shop my_third_model id 1 YES INT64 NEVER None None NO None NO NO NaN NULL
source0.schema
table_catalog table_schema table_name column_name ordinal_position is_nullable data_type is_generated generation_expression is_stored is_hidden is_updatable is_system_defined is_partitioning_column clustering_ordinal_position collation_name
0 bigquery-public-data ml_datasets iris sepal_length 1 YES FLOAT64 NEVER None None NO None NO NO NaN NULL
1 bigquery-public-data ml_datasets iris sepal_width 2 YES FLOAT64 NEVER None None NO None NO NO NaN NULL
2 bigquery-public-data ml_datasets iris petal_length 3 YES FLOAT64 NEVER None None NO None NO NO NaN NULL
3 bigquery-public-data ml_datasets iris petal_width 4 YES FLOAT64 NEVER None None NO None NO NO NaN NULL
4 bigquery-public-data ml_datasets iris species 5 YES STRING NEVER None None NO None NO NO NaN NULL