简体   繁体   English

Get data as dataframe from Look through API sdk package in python

[英]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.

相关问题 如何通过facebook-sdk python api获取用户帖子? - How to get user posts through facebook-sdk python api? 如何在数据框python中查找和填写数据? - How to look up and fill in data in dataframe python? python - 从 API 读取数据并保存到 dataframe - python - read data from API and save into dataframe 如何通过 API 从 amoCRM 获取数据? - How to get data from amoCRM through API? 从 Object 数据类型中获取数据 python dataframe - Get data from a Object datatype in python dataframe Python:从数据帧熊猫中获取随机数据 - Python : get random data from dataframe pandas 从 dataframe python 列中的数据获取国家/地区 - Get country from data in column in dataframe python 如何将 CSV 数据(从 GoogleSheets - Google Python API 获得)转换为表格格式(数据框)? - How to get CSV data (as obtained from GoogleSheets - Google Python API) into a table-format (dataframe)? 如何在Python中循环执行GET请求以从数据框中的分页API接收所有数据 - How to loop a GET request in Python to receive all data from a paginated API in a dataframe 如何通过Python中的序列从数据帧中获取公共变量 - How to get common variables from dataframe through series in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM