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

docs: Simple example of creating a dashboard filter and applying it t… #994

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The full details of all Looker API endpoints are listed in Looker Docs: [Version
## Dashboard : Manage Dashboards

- [Soft delete dashboard](soft_delete_dashboard.py)
- [Create dashboard filter](create_dashboard_filter.py)

## Query : Run and Manage Queries
- [Kill all running queries](kill_queries.py)
Expand Down
87 changes: 87 additions & 0 deletions examples/python/create_dashboard_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Initialize API/SDK for more info go here: https://pypi.org/project/looker-sdk/
import looker_sdk
from looker_sdk import models
from looker_sdk.sdk.api40.models import DashboardElement, DashboardFilter

sdk = looker_sdk.init40("looker.ini")

def main():
"""This file creates a new dashboard filter, and applies that filtering to all tiles on the dashboard.
Dashboard elements listen on the same field that the dashboard filter is created from.
This example can be modified to create a filter on many dashboards at once if you've added a new field to your LookML,
dynamically generate dashboards, etc.
"""

# Update these with the relevant information for the filter you want to create, and the dashboard
dash_id = '<dashboard_id>'
filter_name = '<name_of_filter>'
filter_model = '<model_name>'
filter_explore = '<explore_name>'
filter_dimension = '<view_name.field_name>' # Requires a fully-scoped dimension

filter = create_filter(dash_id, filter_name, filter_model, filter_explore, filter_dimension)
elements = sdk.dashboard_dashboard_elements(dash_id)
for element in elements:
# Skip elements that are text tiles
# Add other restrictions here to skip updating individual tiles
if element.type != 'text':
update_elements_filters(element, filter)

def create_filter(dash_id: str, filter_name: str, filter_model: str, filter_explore: str , filter_dimension: str ) -> DashboardFilter:
"""Creates a dashboard filter object on the specified dashboard. Filters must be tied to a specific LookML Dimension.

Args:
dash_id (str): ID of the dashboard to create the filter on
name (str): Name/Title of the filter
model (str): Model of the dimension
explore (str): Explore of the dimension
dimension (str): Name of the dimension. Must be in the format 'view_name.field_name'
"""

return sdk.create_dashboard_filter(
body=models.WriteCreateDashboardFilter(
dashboard_id=dash_id,
name=filter_name,
title=filter_name,
type='field_filter', # New dashboards are only compatible with field filters
model=filter_model,
explore=filter_explore,
dimension=filter_dimension,
# Add additional parameters as necessary for allowing multiple values, ui configuration, etc.
)
)

def update_elements_filters(element: DashboardElement, filter: DashboardFilter) -> None:
"""Updates a dashboard element's result maker to include a listener on the new dashboard filter.


Args:
element (DashboardElement): Dashboard element to update with the new filter
filter (DashboardFilter): Dashboard filter the element will listen to
"""
# Keep track of the current result_maker and add to it, otherwise listeners for other filters would be removed
current_filterables = element.result_maker.filterables
element.result_maker.filterables = []
for filterable in current_filterables:
new_listens = filterable.listen

# Add listener for new filter, if model and explore is the same
# You can easily add further restrictions to what tiles or queries within merged results will listen to the new tile
# If you don't want to restrict what tiles or queries this listen to this filter, just remove this if
if filter.model == filterable.model and filter.explore == filterable.view:
new_listens.append(models.ResultMakerFilterablesListen(dashboard_filter_name=filter.name, field=filter.dimension))
filterable.listen = new_listens
# Append the new filterables to a result maker that we can use for the dashboard element
element.result_maker.filterables.append(
models.ResultMakerFilterables(
model=filterable.model,
view=filterable.view,
listen=new_listens,
)
)

# Update element with the new result maker that listens to new filter
sdk.update_dashboard_element(dashboard_element_id = element.id, body = models.WriteDashboardElement(result_maker = element.result_maker))

if __name__ == "__main__":
main()