Skip to content

Commit

Permalink
Example click API usage (#6307)
Browse files Browse the repository at this point in the history
* Example python lib w click, written docs

Co-authored-by: Chenyu Li <chenyu.li@dbtlabs.com>
  • Loading branch information
MichelleArk and ChenyuLInx authored Jan 3, 2023
1 parent b0909b8 commit cc5a38e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/dbt/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .main import cli as dbt_cli # noqa
16 changes: 16 additions & 0 deletions core/dbt/cli/context.py
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
20 changes: 20 additions & 0 deletions core/dbt/cli/example.py
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)
32 changes: 32 additions & 0 deletions core/dbt/docs/source/index.rst
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

0 comments on commit cc5a38e

Please sign in to comment.