Looker API: To get list of all dashboards( including LookML dashboards)

A Python script to get list of all dashboards (including LookML dashboards). The information that is retrieved includes the model, folder, and explore that the dashboard is based on.

 

 

 

 

import csv
import looker_sdk
sdk = looker_sdk.init40(config_file='../looker.ini', section='LOOKER')

'''
This script will let you generate an dashboards.csv file .

To use, please follow the instructions below.

1) Create a looker.ini file with 
    [LOOKER]
    # Base URL for API. Do not include /api/* in the url. If hosted on GCP, remove the :19999 leaving just https://your.cloud.looker.com
    base_url=https://your.cloud.looker.com
    # API  client id
    client_id=#######################
    # API  client secret
    client_secret=########################
    # Set to false if testing locally against self-signed certs. Otherwise leave True
    verify_ssl=True

2) Run this Python file directly. It will produce a file called dashboards.csv.
'''

def get_all_dashboards(shared_only=True): 
    """ get highlevel information of all dashboards """
    all_dashboards = sdk.all_dashboards(fields="id, title, description,folder")
    if shared_only:
        dashboards = [
            dashboard for dashboard in all_dashboards
            if  not dashboard.folder.is_personal
            and not dashboard.folder.is_personal_descendant
        ]
        return dashboards
    return all_dashboards

def get_dashboard_by_id(dashboard_id):
    """ get granular information of a dashboard  """
    dashboard=sdk.dashboard(dashboard_id=dashboard_id)
    models, explores = set(), set()
    if dashboard.id.isdigit(): # User Defined Dashboards(UDD)
        for elements in dashboard.dashboard_elements:
            if elements.id and elements.query:
                models.add(elements.query.model)
                explores.add(elements.query.view)
    else: # LookML dashboards
        for elements in dashboard.dashboard_elements:
            if elements.result_maker and elements.result_maker.query_id:
                models.add(elements.result_maker.query.model)
                explores.add(elements.result_maker.query.view)
 
    dashboard.models = models
    dashboard.explores = explores
    return dashboard


with open("dashboards.csv", "w") as output_file: #update me with your output file name
    writer = csv.writer(output_file, delimiter='\t',
                lineterminator='\r\n',
                quotechar = "'")
    writer.writerow([ "Folder Name", "Title", "ID", "Description", "Models","Explores", "Link", "user_id"])
    """ highlevel information of all dashboards """
    dashboards=get_all_dashboards()
    """ granular information of a dashboard (enriched with model and explore infomation) """     
    dashboards = [
                get_dashboard_by_id(dashboard.id) for dashboard in dashboards
            ]
    for dashboard in dashboards:
        writer.writerow([dashboard.folder.name,
                         dashboard.title,
                         dashboard.id,
                         dashboard.description,
                         dashboard.models,
                         dashboard.explores,
                         "https://your.cloud.looker.com/dashboards/"+dashboard.id,  #update me with your looker instance
                         dashboard.user_id])

 

 

 

 

 

0 1 1,160
1 REPLY 1

how to query a LookML dashboard and retrieve its contents