Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example click API usage #6307

Merged
merged 9 commits into from
Jan 3, 2023
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
21 changes: 21 additions & 0 deletions core/dbt/cli/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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"
# Bypass cli group context configuration entirely and invoke deps directly
MichelleArk marked this conversation as resolved.
Show resolved Hide resolved
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`

ChenyuLInx marked this conversation as resolved.
Show resolved Hide resolved
API documentation
-----------------

.. dbt_click:: dbt.cli.main:cli