Skip to content

Commit

Permalink
Add opendbt code part-5
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailsimsek committed Jul 7, 2024
1 parent 6ff0e1c commit 6bb1584
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .idea/opendbt.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
The `opendbt` library extends the capabilities of dbt. It unlocks many customizations, allowing you to tailor dbt to
your specific needs and data workflows.

Forexample create custom transformations by customizing existing adapters
Forexample creating custom transformations by customizing existing adapters

With `opendbt` you can go beyond the core functionalities of dbt by seamlessly integrating your customized adapter and
providing jinja with
custom Python methods tailored to your advanced needs.
provide jinja with further adapter/python methods. Enabling custom python methods tailored to your advanced needs.

# Sample use cases, examples

- Use customised adapter, provide jinja with custom python methods
- Execute Python Model(Python code) Locally
- Enable Model-Level Orchestration Using Airflow
- Create page on Airflow Server to serve DBT docs

please see [examples](docs/EXAMPLES.md).

Expand Down
18 changes: 17 additions & 1 deletion docs/EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,20 @@ from opendbt.airflow import OpenDbtAirflowProject
p = OpenDbtAirflowProject(resource_type='test', project_dir="/dbt/project_dir", profiles_dir="/dbt/profiles_dir",
target='dev', tag="MY_TAG")
p.load_dbt_tasks(dag=dag, start_node=start, end_node=end)
```
```

#### Create page on Airflow Server to serve DBT docs

While its very practical to use airflow for dbt executions, it can be also used to server dbt docs.

here is how:
**Step-1:** Create python file under airflow `/{airflow}/plugins` directory, with following code.
Adjust the given path to the folder where dbt docs are published

https://github.com/memiiso/opendbt/blob/154b3e26981d157da70ebb98f1a1576f1fa55832/tests/resources/airflow/plugins/airflow_dbtdocs_page.py#L1-L6

**Step-2:** Restart airflow, and check that new link `DBT Docs` is created.
![airflow-dbt-docs-link.png](assets%2Fairflow-dbt-docs-link.png)

**Step-3:** open the link and browse dbt docs
![airflow-dbt-docs-page.png](assets%2Fairflow-dbt-docs-page.png)
Binary file added docs/assets/airflow-dbt-docs-link.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/airflow-dbt-docs-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions opendbt/airflow/dbtdocs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from pathlib import Path


def init_plugins_dbtdocs_page(dbt_docs_dir: Path):
from airflow.plugins_manager import AirflowPlugin
from flask import Blueprint
from flask_appbuilder import BaseView, expose

class DBTDocsView(BaseView):
# Use it like this if you want to restrict your view to readonly::
base_permissions = ['can_list', 'can_show']
default_view = "index"

@expose("/")
def index(self):
return dbt_docs_dir.joinpath("index.html").read_text()
# return self.render_template("index.html", content="")

# Creating a flask blueprint to integrate the templates and static folder
bp = Blueprint(
"DBT Docs Plugin",
__name__,
template_folder=dbt_docs_dir.as_posix(),
static_folder=dbt_docs_dir.as_posix(),
static_url_path='/dbtdocsview'
)

v_header_menu = {"name": "DBT Docs", "category": "", "view": DBTDocsView()}

class AirflowDbtDocsPlugin(AirflowPlugin):
name = "DBT Docs Plugin"
flask_blueprints = [bp]
appbuilder_views = [v_header_menu]

return AirflowDbtDocsPlugin
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'opendbt = opendbt:main',
],
},
version='0.1.0',
version='0.2.0',
packages=find_packages(),
author="Memiiso Organization",
description='Python opendbt',
Expand Down
1 change: 1 addition & 0 deletions tests/resources/airflow/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
- ./airflow/webserver_config.py:/opt/airflow/webserver_config.py
- ./airflow/airflow.cfg:/opt/airflow/airflow.cfg
- ./dags:/opt/airflow/dags:rw
- ./plugins:/opt/airflow/plugins:rw
- ./../dbttest:/opt/dbttest:rw
- ./../../../opendbt/macros:/opt/dbttest/macros:rw
environment:
Expand Down
6 changes: 6 additions & 0 deletions tests/resources/airflow/plugins/airflow_dbtdocs_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pathlib import Path

from opendbt.airflow import dbtdocs

# create public page on airflow server to serve DBT docs
airflow_dbtdocs_page = dbtdocs.init_plugins_dbtdocs_page(Path("/opt/dbttest/target"))
4 changes: 4 additions & 0 deletions tests/test_opendbt_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ def test_run_compile(self):
def test_run_run(self):
dp = OpenDbtProject(project_dir=self.DBTTEST_DIR, profiles_dir=self.DBTTEST_DIR)
dp.run(command="run", args=['--select', 'my_first_dbt_model+'], use_subprocess=True)

def test_run_docs_generate(self):
dp = OpenDbtProject(project_dir=self.DBTTEST_DIR, profiles_dir=self.DBTTEST_DIR)
dp.run(command="docs", args=["generate"])

0 comments on commit 6bb1584

Please sign in to comment.