如何在Python中使用LookerSDK 4.0下载已应用过滤器的仪表板上的瓷砖?

huangapple go评论92阅读模式
英文:

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

问题

我需要以Python编程方式从Looker仪表板中下载特定元素(图表和表格)。我目前正在探索LookerSDK 4.0以实现我的用例。

我按照这些示例来下载带有应用过滤器的仪表板以及单独下载瓷砖。
https://github.com/looker-open-source/sdk-codegen/tree/main/examples/python

我尝试通过应用过滤器来更新仪表板,

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

# 使用仪表板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

# 获取带有过滤器的更新后的仪表板
dashboard = sdk.update_dashboard(id, dashboard)

然后根据瓷砖名称获取瓷砖,

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

然后下载瓷砖,

task = sdk.create_query_render_task(
    query_id=tile.query_id, 
    result_format=format,
    width=600, 
    height=600
    )
# 轮询渲染任务直到完成的代码
...
...
...

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

当我尝试更新仪表板过滤器并下载瓷砖时,它仍然以没有过滤器的方式下载瓷砖图像。有人可以帮助我理解如何实现这一目标吗?

英文:

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?

答案1

得分: 0

我找到了解决上述问题的方法。我们需要使用特定的筛选器更新仪表板,然后获取瓷砖。

创建仪表板筛选器对象。

updated_filter = models.WriteDashboardFilter(
default_value="<要更新的值>",
model="<模型名称>",
name="<字段名称>",
type="<字段类型>"
)

使用筛选器更新仪表板

sdk.update_dashboard_filter(dashboard_filter_id, updated_filter)

然后,您可以使用更新后的仪表板对象获取特定的瓷砖。请改用create_dashboard_element_render_task而不是create_query_render_task。

创建一个渲染瓷砖到图像/文档的仪表板元素任务

task = sdk.create_dashboard_element_render_task(
query_id=tile.query_id,
result_format=format,
width=600,
height=600
)

英文:

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=&quot;&lt;value to be updated&gt;&quot;,
    model=&quot;&lt;model name&gt;&quot;,
    name=&quot;&lt;field name&gt;&quot;,
    type=&quot;&lt;field type&gt;&quot;
)

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

huangapple
  • 本文由 发表于 2023年5月25日 19:18:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定