Skip to content

Commit 3412dbc

Browse files
Kevin DunnKevin Dunn
authored andcommitted
docs: Simple example of creating a dashboard filter and applying it to dashboard elements (#818)
* Pushing new_dashboard_filter to remote * Adding example to create_dashboard_filter, fixes 816 * Rename dashboard_filter file * add new line at end of file * Fixing new line at end of file 🤦🏼‍♂️ * Condensing loops * Consolidating loops, adding check for matching model and explore * Removing unnecessary loop Co-authored-by: Kevin Dunn <[email protected]>
1 parent 483ab43 commit 3412dbc

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

Diff for: examples/python/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The full details of all Looker API endpoints are listed in Looker Docs: [Version
2222
## Dashboard : Manage Dashboards
2323

2424
- [Soft delete dashboard](soft_delete_dashboard.py)
25+
- [Create dashboard filter](create_dashboard_filter.py)
2526

2627
## Query : Run and Manage Queries
2728
- [Kill all running queries](kill_queries.py)

Diff for: examples/python/create_dashboard_filter.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Initialize API/SDK for more info go here: https://pypi.org/project/looker-sdk/
2+
import looker_sdk
3+
from looker_sdk import models
4+
from looker_sdk.sdk.api40.models import DashboardElement, DashboardFilter
5+
6+
sdk = looker_sdk.init40("looker.ini")
7+
8+
def main():
9+
"""This file creates a new dashboard filter, and applies that filtering to all tiles on the dashboard.
10+
Dashboard elements listen on the same field that the dashboard filter is created from.
11+
This example can be modified to create a filter on many dashboards at once if you've added a new field to your LookML,
12+
dynamically generate dashboards, etc.
13+
"""
14+
15+
# Update these with the relevant information for the filter you want to create, and the dashboard
16+
dash_id = '<dashboard_id>'
17+
filter_name = '<name_of_filter>'
18+
filter_model = '<model_name>'
19+
filter_explore = '<explore_name>'
20+
filter_dimension = '<view_name.field_name>' # Requires a fully-scoped dimension
21+
22+
filter = create_filter(dash_id, filter_name, filter_model, filter_explore, filter_dimension)
23+
elements = sdk.dashboard_dashboard_elements(dash_id)
24+
for element in elements:
25+
# Skip elements that are text tiles
26+
# Add other restrictions here to skip updating individual tiles
27+
if element.type != 'text':
28+
update_elements_filters(element, filter)
29+
30+
def create_filter(dash_id: str, filter_name: str, filter_model: str, filter_explore: str , filter_dimension: str ) -> DashboardFilter:
31+
"""Creates a dashboard filter object on the specified dashboard. Filters must be tied to a specific LookML Dimension.
32+
33+
Args:
34+
dash_id (str): ID of the dashboard to create the filter on
35+
name (str): Name/Title of the filter
36+
model (str): Model of the dimension
37+
explore (str): Explore of the dimension
38+
dimension (str): Name of the dimension. Must be in the format 'view_name.field_name'
39+
"""
40+
41+
return sdk.create_dashboard_filter(
42+
body=models.WriteCreateDashboardFilter(
43+
dashboard_id=dash_id,
44+
name=filter_name,
45+
title=filter_name,
46+
type='field_filter', # New dashboards are only compatible with field filters
47+
model=filter_model,
48+
explore=filter_explore,
49+
dimension=filter_dimension,
50+
# Add additional parameters as necessary for allowing multiple values, ui configuration, etc.
51+
)
52+
)
53+
54+
def update_elements_filters(element: DashboardElement, filter: DashboardFilter) -> None:
55+
"""Updates a dashboard element's result maker to include a listener on the new dashboard filter.
56+
57+
58+
Args:
59+
element (DashboardElement): Dashboard element to update with the new filter
60+
filter (DashboardFilter): Dashboard filter the element will listen to
61+
"""
62+
# Keep track of the current result_maker and add to it, otherwise listeners for other filters would be removed
63+
current_filterables = element.result_maker.filterables
64+
element.result_maker.filterables = []
65+
for filterable in current_filterables:
66+
new_listens = filterable.listen
67+
68+
# Add listener for new filter, if model and explore is the same
69+
# You can easily add further restrictions to what tiles or queries within merged results will listen to the new tile
70+
# If you don't want to restrict what tiles or queries this listen to this filter, just remove this if
71+
if filter.model == filterable.model and filter.explore == filterable.view:
72+
new_listens.append(models.ResultMakerFilterablesListen(dashboard_filter_name=filter.name, field=filter.dimension))
73+
filterable.listen = new_listens
74+
# Append the new filterables to a result maker that we can use for the dashboard element
75+
element.result_maker.filterables.append(
76+
models.ResultMakerFilterables(
77+
model=filterable.model,
78+
view=filterable.view,
79+
listen=new_listens,
80+
)
81+
)
82+
83+
# Update element with the new result maker that listens to new filter
84+
sdk.update_dashboard_element(dashboard_element_id = element.id, body = models.WriteDashboardElement(result_maker = element.result_maker))
85+
86+
if __name__ == "__main__":
87+
main()

0 commit comments

Comments
 (0)