[英]Get data as dataframe from Look through API sdk package in python
I have the following code to get a look from Looker through an API.我有以下代码可以通过 API 从 Looker 中查看。 I stored the api in a.ini.
我将 api 存储在 a.ini 中。 All of that works, but now I want to get the data from this look as Dataframe in python, so that I can use the data for further analysis.
所有这些都有效,但现在我想从 python 中的 Dataframe 中获取数据,以便我可以使用这些数据进行进一步分析。 How can i do that?
我怎样才能做到这一点? I used this code, but that only saves it to png.
我使用了此代码,但这仅将其保存为 png。 I can't find a way to create a dataframe from this, as I want the data itself and not just the outcome image.
我找不到从中创建 dataframe 的方法,因为我想要数据本身而不仅仅是结果图像。
import sys
import textwrap
import time
import looker_sdk
from looker_sdk import models
sdk = looker_sdk.init40("/Name.ini")
def get_look(title: str) -> models.Look:
title = title.lower()
look = next(iter(sdk.search_looks(title=title)), None)
if not look:
raise Exception(f"look '{title}' was not found")
return look
def download_look(look: models.Look, result_format: str, width: int, height: int):
"""Download specified look as png/jpg"""
id = int(look.id)
task = sdk.create_look_render_task(id, result_format, width, height,)
if not (task and task.id):
raise sdk.RenderTaskError(
f"Could not create a render task for '{look.title}'"
)
# poll the render task until it completes
elapsed = 0.0
delay = 0.5 # wait .5 seconds
while True:
poll = sdk.render_task(task.id)
if poll.status == "failure":
print(poll)
raise Exception(f"Render failed for '{look.title}'")
elif poll.status == "success":
break
time.sleep(delay)
elapsed += delay
print(f"Render task completed in {elapsed} seconds")
result = sdk.render_task_results(task.id)
filename = f"{look.title}.{result_format}"
with open(filename, "wb") as f:
f.write(result)
print(f"Look saved to '{filename}'")
look_title = sys.argv[1] if len(sys.argv) > 1 else "Name"
image_width = int(sys.argv[2]) if len(sys.argv) > 2 else 545
image_height = int(sys.argv[3]) if len(sys.argv) > 3 else 842
image_format = sys.argv[4] if len(sys.argv) > 4 else "png"
if not look_title:
raise Exception(
textwrap.dedent(
"""
Please provide: <lookTitle> [<img_width>] [<img_height>] [<img_format>]
img_width defaults to 545
img_height defaults to 842
img_format defaults to 'png'"""
)
)
look = get_look(look_title)
#Dataframe storage
download_look(look, image_format, image_width, image_height)
The SDK function you are using ( create_look_render_task
), which is described here only allows you to download in either pdf, png, or jpg.您正在使用的 SDK function (
create_look_render_task
), 此处描述的仅允许您以 pdf 或 jpg 格式下载。
If you want to get the data from a Look into a dataframe then you may want to look into using the run_look
function instead described here .如果您想从查看 dataframe 中获取数据,那么您可能希望使用
run_look
function 进行查看,而不是在此处描述。 When you use run_look
you can change the result_format
to CSV and then write your own code to convert to a dataframe.当您使用
run_look
时,您可以将result_format
更改为 CSV,然后编写您自己的代码以转换为 dataframe。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.