From 94bcd2a42a9f37f65613e21ee8b5704ef94d6204 Mon Sep 17 00:00:00 2001
From: Maxime Armstrong <46797220+maximearmstrong@users.noreply.github.com>
Date: Mon, 25 Nov 2024 15:20:44 -0500
Subject: [PATCH] [dagster-tableau] Update custom translator example in Tableau
docs (#26029)
## Summary & Motivation
Update the custom translator example in the Tableau docs to reflect
`get_asset_spec().key`.
This PR stack will be merged after #25941 lands, so that
`replace_attributes()` can be called as
`AssetSpec().replace_attributes()`
---
docs/content/integrations/tableau.mdx | 23 +++++++++++++++----
.../tableau/customize-tableau-asset-defs.py | 19 +++++++++++----
2 files changed, 33 insertions(+), 9 deletions(-)
diff --git a/docs/content/integrations/tableau.mdx b/docs/content/integrations/tableau.mdx
index 5c1ba6833e633..ae7bf301d5147 100644
--- a/docs/content/integrations/tableau.mdx
+++ b/docs/content/integrations/tableau.mdx
@@ -104,7 +104,7 @@ defs = dg.Definitions(assets=[*tableau_specs], resources={"tableau": tableau_wor
### Customize asset definition metadata for Tableau assets
-By default, Dagster will generate asset keys for each Tableau asset based on its type and name and populate default metadata. You can further customize asset properties by passing a custom subclass to the function. This subclass can implement methods to customize the asset keys or specs for each Tableau asset type.
+By default, Dagster will generate asset specs for each Tableau asset based on its type, and populate default metadata. You can further customize asset properties by passing a custom subclass to the function. This subclass can implement methods to customize the asset specs for each Tableau asset type.
```python file=/integrations/tableau/customize-tableau-asset-defs.py
from dagster_tableau import (
@@ -112,7 +112,7 @@ from dagster_tableau import (
TableauCloudWorkspace,
load_tableau_asset_specs,
)
-from dagster_tableau.translator import TableauContentData
+from dagster_tableau.translator import TableauContentData, TableauContentType
import dagster as dg
@@ -129,9 +129,20 @@ tableau_workspace = TableauCloudWorkspace(
# A translator class lets us customize properties of the built
# Tableau assets, such as the owners or asset key
class MyCustomTableauTranslator(DagsterTableauTranslator):
- def get_sheet_spec(self, data: TableauContentData) -> dg.AssetSpec:
- # We add a custom team owner tag to all sheets
- return super().get_sheet_spec(data)._replace(owners=["team:my_team"])
+ def get_asset_spec(self, data: TableauContentData) -> dg.AssetSpec:
+ # We create the default asset spec using super()
+ default_spec = super().get_asset_spec(data)
+ # We customize the metadata and asset key prefix for all assets, including sheets,
+ # and we customize the team owner tag only for sheets.
+ return default_spec.replace_attributes(
+ key=default_spec.key.with_prefix("prefix"),
+ metadata={**default_spec.metadata, "custom": "metadata"},
+ owners=(
+ ["team:my_team"]
+ if data.content_type == TableauContentType.SHEET
+ else ...
+ ),
+ )
tableau_specs = load_tableau_asset_specs(
@@ -140,6 +151,8 @@ tableau_specs = load_tableau_asset_specs(
defs = dg.Definitions(assets=[*tableau_specs], resources={"tableau": tableau_workspace})
```
+Note that `super()` is called in each of the overridden methods to generate the default asset spec. It is best practice to generate the default asset spec before customizing it.
+
### Load Tableau assets from multiple workspaces
Definitions from multiple Tableau workspaces can be combined by instantiating multiple Tableau resources and merging their specs. This lets you view all your Tableau assets in a single asset graph:
diff --git a/examples/docs_snippets/docs_snippets/integrations/tableau/customize-tableau-asset-defs.py b/examples/docs_snippets/docs_snippets/integrations/tableau/customize-tableau-asset-defs.py
index 9bf1e95642fe5..d01c5e50f532f 100644
--- a/examples/docs_snippets/docs_snippets/integrations/tableau/customize-tableau-asset-defs.py
+++ b/examples/docs_snippets/docs_snippets/integrations/tableau/customize-tableau-asset-defs.py
@@ -3,7 +3,7 @@
TableauCloudWorkspace,
load_tableau_asset_specs,
)
-from dagster_tableau.translator import TableauContentData
+from dagster_tableau.translator import TableauContentData, TableauContentType
import dagster as dg
@@ -20,9 +20,20 @@
# A translator class lets us customize properties of the built
# Tableau assets, such as the owners or asset key
class MyCustomTableauTranslator(DagsterTableauTranslator):
- def get_sheet_spec(self, data: TableauContentData) -> dg.AssetSpec:
- # We add a custom team owner tag to all sheets
- return super().get_sheet_spec(data)._replace(owners=["team:my_team"])
+ def get_asset_spec(self, data: TableauContentData) -> dg.AssetSpec:
+ # We create the default asset spec using super()
+ default_spec = super().get_asset_spec(data)
+ # We customize the metadata and asset key prefix for all assets, including sheets,
+ # and we customize the team owner tag only for sheets.
+ return default_spec.replace_attributes(
+ key=default_spec.key.with_prefix("prefix"),
+ metadata={**default_spec.metadata, "custom": "metadata"},
+ owners=(
+ ["team:my_team"]
+ if data.content_type == TableauContentType.SHEET
+ else ...
+ ),
+ )
tableau_specs = load_tableau_asset_specs(