Skip to content

Commit

Permalink
[dagster-tableau] Update Tableau docs with custom materialization exa…
Browse files Browse the repository at this point in the history
…mple (#25672)

## Summary & Motivation

As title.
  • Loading branch information
maximearmstrong authored Nov 5, 2024
1 parent f84b5b8 commit b7dafbd
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
66 changes: 66 additions & 0 deletions docs/content/integrations/tableau.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,72 @@ defs = dg.Definitions(
)
```

### Customizing how Tableau assets are materialized

Instead of using the out-of-the-box <PyObject module="dagster_tableau" method="build_tableau_materializable_assets_definition" /> utility, you can build your own assets definition that trigger the refresh of your Tableau workbooks. This allows you to customize how the refresh is triggered or to run custom code before or after the refresh.

```python file=/integrations/tableau/materialize-tableau-assets-advanced.py
from typing import Sequence

from dagster_tableau import (
TableauCloudWorkspace,
load_tableau_asset_specs,
parse_tableau_external_and_materializable_asset_specs,
)

import dagster as dg

tableau_workspace = TableauCloudWorkspace(
connected_app_client_id=dg.EnvVar("TABLEAU_CONNECTED_APP_CLIENT_ID"),
connected_app_secret_id=dg.EnvVar("TABLEAU_CONNECTED_APP_SECRET_ID"),
connected_app_secret_value=dg.EnvVar("TABLEAU_CONNECTED_APP_SECRET_VALUE"),
username=dg.EnvVar("TABLEAU_USERNAME"),
site_name=dg.EnvVar("TABLEAU_SITE_NAME"),
pod_name=dg.EnvVar("TABLEAU_POD_NAME"),
)


# Assets definition factory which triggers workbooks refresh and sends a notification once complete
def build_tableau_materialize_and_notify_asset_def(
specs: Sequence[dg.AssetSpec], refreshable_workbook_ids: Sequence[str]
) -> dg.AssetsDefinition:
@dg.multi_asset(
name="tableau_sync",
compute_kind="tableau",
specs=specs,
)
def asset_fn(context: dg.AssetExecutionContext, tableau: TableauCloudWorkspace):
with tableau.get_client() as client:
yield from client.refresh_and_materialize_workbooks(
specs=specs, refreshable_workbook_ids=refreshable_workbook_ids
)
# Do some custom work after refreshing here, such as sending an email notification

return asset_fn


# Load Tableau asset specs
tableau_specs = load_tableau_asset_specs(
workspace=tableau_workspace,
)

external_asset_specs, materializable_asset_specs = (
parse_tableau_external_and_materializable_asset_specs(tableau_specs)
)

# Use the asset definition builder to construct the definition for tableau materializable assets
defs = dg.Definitions(
assets=[
build_tableau_materialize_and_notify_asset_def(
specs=materializable_asset_specs,
refreshable_workbook_ids=["b75fc023-a7ca-4115-857b-4342028640d0"],
),
*external_asset_specs,
],
resources={"tableau": tableau_workspace},
)
```

### Related

- [`dagster-tableau` API reference](/\_apidocs/libraries/dagster-tableau)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from typing import Sequence

from dagster_tableau import (
TableauCloudWorkspace,
load_tableau_asset_specs,
parse_tableau_external_and_materializable_asset_specs,
)

import dagster as dg

tableau_workspace = TableauCloudWorkspace(
connected_app_client_id=dg.EnvVar("TABLEAU_CONNECTED_APP_CLIENT_ID"),
connected_app_secret_id=dg.EnvVar("TABLEAU_CONNECTED_APP_SECRET_ID"),
connected_app_secret_value=dg.EnvVar("TABLEAU_CONNECTED_APP_SECRET_VALUE"),
username=dg.EnvVar("TABLEAU_USERNAME"),
site_name=dg.EnvVar("TABLEAU_SITE_NAME"),
pod_name=dg.EnvVar("TABLEAU_POD_NAME"),
)


# Assets definition factory which triggers workbooks refresh and sends a notification once complete
def build_tableau_materialize_and_notify_asset_def(
specs: Sequence[dg.AssetSpec], refreshable_workbook_ids: Sequence[str]
) -> dg.AssetsDefinition:
@dg.multi_asset(
name="tableau_sync",
compute_kind="tableau",
specs=specs,
)
def asset_fn(context: dg.AssetExecutionContext, tableau: TableauCloudWorkspace):
with tableau.get_client() as client:
yield from client.refresh_and_materialize_workbooks(
specs=specs, refreshable_workbook_ids=refreshable_workbook_ids
)
# Do some custom work after refreshing here, such as sending an email notification

return asset_fn


# Load Tableau asset specs
tableau_specs = load_tableau_asset_specs(
workspace=tableau_workspace,
)

external_asset_specs, materializable_asset_specs = (
parse_tableau_external_and_materializable_asset_specs(tableau_specs)
)

# Use the asset definition builder to construct the definition for tableau materializable assets
defs = dg.Definitions(
assets=[
build_tableau_materialize_and_notify_asset_def(
specs=materializable_asset_specs,
refreshable_workbook_ids=["b75fc023-a7ca-4115-857b-4342028640d0"],
),
*external_asset_specs,
],
resources={"tableau": tableau_workspace},
)

1 comment on commit b7dafbd

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for dagster-docs ready!

✅ Preview
https://dagster-docs-mgn07a0jb-elementl.vercel.app
https://master.dagster.dagster-docs.io

Built with commit b7dafbd.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.