Python wrapper for dbt-core to extend dbt with custom Python.
Project description
dbt-π 🧬
Python wrapper for dbt-core to extend dbt with custom Python.
Shimmy shimmy shim 🕺🕺🕺
This package is a shim for dbt-core, inspired by (cough stolen from cough) my old boss, @darkdreamingdan:
Before using this package, it's recommended to get up to speed with the Python modules that are already available in dbt:
The existing Python modules are available in the dbt Jinja context under the modules object, for example:
{{ modules.datetime.datetime.now() }}
Installation ⬇️
While in preview, this package is only available from GitHub:
pip install git+https://github.com/Bilbottom/dbt-py@v0.0.3
This will be made available on PyPI once it's ready for general use.
Usage 📖
This package adds a new executable, dbt-py, which injects your custom Python into dbt and then runs dbt. Either a custom module or a custom package can be injected. A custom module is the simplest to get started with.
The default module/package name is custom which would make custom Python available in the dbt Jinja context under the modules.custom object. This can be configured (see the Configuration section below).
Custom Module 🐍
Create a module called custom.py in the root of your dbt project. This module can contain any Python code you like, for example:
def salutation(name: str) -> str:
return f"Hello, {name}!"
Reference this module and function in the dbt Jinja context of a dbt model:
{{ modules.custom.salutation("World") }}
Rather than run dbt with the dbt command, instead run it with dbt-py:
dbt-py clean
dbt-py build
Note that dbt-py is a wrapper around dbt so all the usual dbt commands are available -- all the arguments passed to dbt-py are passed through to dbt, too.
dbt-py --help
dbt-py run --select my_model
dbt-py test --select tag:unit-test
Custom Package 📦
Using a custom package is similar to using a custom module: create a package called custom in the root of your dbt project.
The submodules of this package will be available in the dbt Jinja context too. For example, suppose you have a package called custom with a submodule called greetings:
custom/
__init__.py
greetings.py
If the greetings.py submodule contains the same salutation function as above, then it can be referenced in the dbt Jinja context as follows:
{{ modules.custom.greetings.salutation("World") }}
Alternatively, you can expose the salutation function via the __init__.py file and then reference it directly via custom:
{{ modules.custom.salutation("World") }}
Configuration 🛠️
The default module/package and Jinja context name is custom but both can be configured with the following environment variables:
DBT_PY_PACKAGE_ROOT: The Python-style ref to the custom module/package, e.g.package.module.submoduleDBT_PY_PACKAGE_NAME: The name to give the custom module/package in the dbt Jinja context, e.g.custom_py. Defaults to the value ofDBT_PY_PACKAGE_ROOT
In particular, you can use the DBT_PY_PACKAGE_ROOT environment variable to reference a custom module/package that is not at the root of your dbt project.
[!WARNING]
If you set the
DBT_PY_PACKAGE_ROOTenvironment variable to a name that already exists, this package will use the existing module/package rather than your custom one. Make sure that your custom module/package name does not clash with any existing modules/packages.This is likely to change in a future release, but for now you may choose to exploit this behaviour to use an existing module/package in your dbt Jinja context. For example, you could set
DBT_PY_PACKAGE_ROOTtomathand then reference themathstandard library in your dbt Jinja context:{{ modules.math.pi }}
Future Work 🚧
This is still in preview, and there are a few things to be added before it's ready for general use:
- Support for importing any number of packages (currently only one package is supported)
- Configuration via config files and CLI arguments (currently only environment variables are supported)
- More robust testing
Contributing 🤝
Raise an issue, or fork the repo and open a pull request.
This project uses Poetry for dependency management and pre-commit for linting. After cloning the repo, install the dependencies and enable pre-commit:
poetry install --sync --with dev,test
pre-commit install --install-hooks
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dbt_py_wrap-0.0.3.tar.gz.
File metadata
- Download URL: dbt_py_wrap-0.0.3.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c124f7278d097959f540a075099475a6975a5839b8a7c96eb1ccbbe36b0c803
|
|
| MD5 |
542cbf710510d6ba5a74304529d5d98f
|
|
| BLAKE2b-256 |
fc1dd1f729607e0cd07a5bd136f929301db51892f7f072d80bd6bdd57f2a5478
|
File details
Details for the file dbt_py_wrap-0.0.3-py3-none-any.whl.
File metadata
- Download URL: dbt_py_wrap-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3de1d949d9582eb2faf3c1262ac6c3392fe1bae49739fca4982a29cbd6b54671
|
|
| MD5 |
5d078763b57d8fdb80cb90b029c3bd44
|
|
| BLAKE2b-256 |
f22773a83365be7263e24fcdb22dc0dadad988c7305ca634691168bca7c06d30
|