1

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

Saran
  • 835
  • 3
  • 11
  • 31

2 Answers2

1

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)
0

I used the api to export the infos for all dashboards with a query like this: http://localhost:8088/api/v1/dashboard/export/?q=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

You can generate it with python

print(f"https://yourdomain/api/v1/dashboard/export/?q={[i for i in range(100)]}")

Then I used a python script to write the relevant information in a csv file:

import csv
import json

with open("your_api_file.json") as file:
    data = json.load(file)

dashboards_info: list[tuple] = [("dashboard", "chart", "typ", "datasource")]
dashboards = data["dashboards"]
for entry_1 in dashboards:
    dashboard = entry_1["__Dashboard__"]
    if "slices" not in dashboard:
        print(f"No charts in {dashboard['dashboard_title']}")
        continue
    for entry_2 in dashboard["slices"]:
        chart = entry_2["__Slice__"]
        dashboards_info.append(
            (
                dashboard["dashboard_title"],
                chart["slice_name"],
                chart["viz_type"],
                chart["datasource_name"],
            )
        )

with open("dashboards_info.csv", "w", newline="") as file:
    csv_writer = csv.writer(file)
    csv_writer.writerows(dashboards_info)

The result looks like this:

dashboard,chart,typ,datasource
my_dashboard_1,my_chart_1,table,datatable_1
my_dashboard_1,my_chart_2,table,datatable_2
my_dashboard_2,my_chart_1,table,datatable_1
my_dashboard_2,my_chart_3,line,datatable_3
Wulf
  • 11
  • 2
  • Looks like a better solution for this problem. However i tried executing the API, and I am getting zip files with multiple yaml file database, datasets, dashboards, chart folders. I am not getting any .json file as you used in the above code. How do you get your_api_file.json file? Note that i use the superset 2.0.1 version – Saran Jul 21 '23 at 09:20
  • Strange. But maybe you can read the yaml file with the dashboards into "data". There is a function that is analog to json.load: https://stackoverflow.com/questions/1773805/how-can-i-parse-a-yaml-file-in-python – Wulf Jul 24 '23 at 05:32