Search code examples
apache-superset

Superset: How to find all dashboard using a specific chart


Is there a way to find all dashboards using a specific chart in Apache superset? I am using the 2.0.1 version.


Solution

  • You can get this information using the Apache Superset REST API:

    /api/v1/chart/{pk}
    

    Example: Get all dashboards for the chart with id=42. In line 26 you can see the dashboards array. So the chart lives in dashboard with id=1.

    Result of the API call: http://localhost:8088/api/v1/chart/42.

    result of http://localhost:8088/api/v1/chart/42

    Edit:

    Here is an example to retrieve this information with Python:

    def get_chart_info(chart_id):
        endpoint = f'http://localhost:8088/api/v1/chart/{chart_id}'
        headers = {
            'X-CSRFToken': "token",
            'Authorization': "Bearer token"
        }
        response = requests.get(endpoint, headers=headers)
        return response.json()
    
    chart_id = 42
    chart_info = get_chart_info(chart_id)
    
    dashboards = chart_info['result']['dashboards']
    dashboard_info = [(dashboard['id'], dashboard['dashboard_title']) for dashboard in dashboards]
    print(dashboard_info)
    

    the result is:

    [(1, 'World Bank's Data'), ...]

    Full working example:

    import requests
    
    base_url = 'http://localhost:8088/api/v1'
    
    def get_bearer_token():
        payload = {
            "password": "admin",
            "provider": "db",
            "username": "admin"
        }
        headers = {
            "Content-Type": "application/json"
        }
        response = requests.request("POST", f"{base_url}/security/login", json=payload, headers=headers)
        return response.json()["access_token"]
    
    def get_csrf_token():
        payload = ""
        headers = {
            "Authorization": f"Bearer {get_bearer_token()}"
        }
        response = requests.request("GET", f"{base_url}/security/csrf_token/", data=payload, headers=headers)
        return response.json()["result"]
    
    def get_chart_info(chart_id):
        headers = {
            'X-CSRFToken': get_csrf_token(),
            'Authorization': f"Bearer {get_bearer_token()}"
        }
        response = requests.get(f"{base_url}/chart/{chart_id}", headers=headers)
        return response.json()
    
    chart_id = 42
    chart_info = get_chart_info(chart_id)
    
    dashboards = chart_info['result']['dashboards']
    dashboard_info = [(dashboard['id'], dashboard['dashboard_title']) for dashboard in dashboards]
    print(dashboard_info)