-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Example python lib w click, written docs Co-authored-by: Chenyu Li <chenyu.li@dbtlabs.com>
- Loading branch information
1 parent
b0909b8
commit cc5a38e
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import cli as dbt_cli # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import click | ||
from typing import Optional | ||
|
||
from dbt.cli.main import cli as dbt | ||
|
||
|
||
def make_context(args, command=dbt) -> Optional[click.Context]: | ||
try: | ||
ctx = command.make_context(command.name, args) | ||
except click.exceptions.Exit: | ||
return None | ||
|
||
ctx.invoked_subcommand = ctx.protected_args[0] if ctx.protected_args else None | ||
ctx.obj = {} | ||
|
||
return ctx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from dbt.cli.main import dbtRunner | ||
from dbt.config.runtime import load_profile, load_project | ||
|
||
if __name__ == "__main__": | ||
project_dir = "/Users/chenyuli/git/jaffle_shop" | ||
cli_args = ["run", "--project-dir", project_dir] | ||
|
||
# initialize the dbt runner | ||
dbt = dbtRunner() | ||
# run the command | ||
res, success = dbt.invoke(cli_args) | ||
|
||
# preload profile and project | ||
profile = load_profile(project_dir, {}, "testing-postgres") | ||
project = load_project(project_dir, False, profile, {}) | ||
|
||
# initialize the runner with pre-loaded profile and project, you can also pass in a preloaded manifest | ||
dbt = dbtRunner(profile=profile, project=project) | ||
# run the command, this will use the pre-loaded profile and project instead of loading | ||
res, success = dbt.invoke(cli_args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,36 @@ | ||
dbt-core's API documentation | ||
============================ | ||
How to invoke dbt commands in python runtime | ||
-------------------------------------------- | ||
|
||
Right now the best way to invoke a command from python runtime is to use the `dbtRunner` we exposed | ||
|
||
.. code-block:: python | ||
from dbt.cli.main import dbtRunner | ||
cli_args = ['run', '--project-dir', 'jaffle_shop'] | ||
# initialize the dbt runner | ||
dbt = dbtRunner() | ||
# run the command | ||
res, success = dbt.invoke(args) | ||
You can also pass in pre constructed object into dbtRunner, and we will use those objects instead of loading up from the disk. | ||
|
||
.. code-block:: python | ||
# preload profile and project | ||
profile = load_profile(project_dir, {}, 'testing-postgres') | ||
project = load_project(project_dir, False, profile, {}) | ||
# initialize the runner with pre-loaded profile and project | ||
dbt = dbtRunner(profile=profile, project=project) | ||
# run the command, this will use the pre-loaded profile and project instead of loading | ||
res, success = dbt.invoke(cli_args) | ||
For the full example code, you can refer to `core/dbt/cli/example.py` | ||
|
||
API documentation | ||
----------------- | ||
|
||
.. dbt_click:: dbt.cli.main:cli |