Package: dbt-py-wrap
dbt-π 🧬Python wrapper fordbt-coreto extend dbt with custom Python.Shimmy shimmy shim 🕺🕺🕺This package is ashimfordbt-core, inspired by (coughstolen fromcough) my old boss,@darkdreamingdan:https://gist.github.com/darkdreamingdan/c5ded709a90fc3c5b420cee5f644f499Before using this package, it's recommended to get up to speed with the Python modules that are already available in dbt:https://docs.getdbt.com/reference/dbt-jinja-functions/modulesThe existing Python modules are available in the dbt Jinja context under themodulesobject, 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.2This 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 iscustomwhich would make custom Python available in the dbt Jinja context under themodules.customobject. This can be configured (see theConfigurationsection below).Custom Module 🐍Create a module calledcustom.pyin the root of your dbt project. This module can contain any Python code you like, for example:defsalutation(name:str)->str:returnf"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 thedbtcommand, instead run it withdbt-py:dbt-py clean
dbt-py buildNote thatdbt-pyis a wrapper arounddbtso all the usual dbt commands are available -- all the arguments passed todbt-pyare passed through todbt, too.dbt-py --help
dbt-py run --select my_model
dbt-py test --select tag:unit-testCustom Package 📦Using a custom package is similar to using a custom module: create a package calledcustomin 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 calledcustomwith a submodule calledgreetings:custom/
    __init__.py
    greetings.pyIf thegreetings.pysubmodule contains the samesalutationfunction as above, then it can be referenced in the dbt Jinja context as follows:{{modules.custom.greetings.salutation("World")}}Alternatively, you can expose thesalutationfunction via the__init__.pyfile and then reference it directly viacustom:{{modules.custom.salutation("World")}}Configuration 🛠️The default module/package and Jinja context name iscustombut 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_ROOTIn particular, you can use theDBT_PY_PACKAGE_ROOTenvironment variable to reference a custom module/package that is not at the root of your dbt project.Warning ⚠️If you set theDBT_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 setDBT_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