pythonsdkimagedownloadlooker

How can I use LookerSDK 4.0 to download a tile from a dashboard with filters applied in Python?


I need to download some specific elements(charts and tables) from a looker dashboard programmatically in python. I am currently exploring the LookerSDK 4.0 to achieve my use case.

I followed these examples to download a dashboard with filters applied and downloading a tile separately. https://github.com/looker-open-source/sdk-codegen/tree/main/examples/python

I have tried updating the dashboard by applying filters,

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

# get dashboard using its id 
dashboard = sdk.dashboard(id)

filters = dashboard.dashboard_filters
for filter in filters:
     if filter["name"] == "user_id":
        filter["default_value"] = "abc"
        break
dashboard.dashboard_filters = filters

# gets updated dashboard with filters
dashboard = sdk.update_dashboard(id, dashboard)

Then get the tile using the dashboard based on the tile name,

title = title.lower()
found = None
for tile in dash.dashboard_elements:
      if tile.title.lower() == title:
        found = tile
        break

And then downloads the tile,

task = sdk.create_query_render_task(
    query_id=tile.query_id, 
    result_format=format,
    width=600, 
    height=600
    )
# code to poll the render task until it completes
...
...
...

fileName = "test.png"
with open(fileName, "wb") as f:
    f.write(result)

When I try to update the dashboard filters and download the tile, it is still downloading the tile image without filters. Can anyone help me understand how we can achieve this?


Solution

  • Found the resolution to the above problem I was facing. We need to update the dashboard with the specific filters and then fetch the tile.

    # create the dashboard filter object.
    updated_filter = models.WriteDashboardFilter(
        default_value="<value to be updated>",
        model="<model name>",
        name="<field name>",
        type="<field type>"
    )
    
    # update dashboard with filter
    sdk.update_dashboard_filter(dashboard_filter_id, updated_filter)
    

    You can then use the updated dashboard object to get the specific tile. Use create_dashboard_element_render_task instead of create_query_render_task.

    # create a dashboard element task to render tile to an image/document
    task = sdk.create_dashboard_element_render_task(
        query_id=tile.query_id, 
        result_format=format,
        width=600, 
        height=600
        )