简体   繁体   中英

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. I stored the api in 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. How can i do that? I used this code, but that only saves it to 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.

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.

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 . When you use run_look you can change the result_format to CSV and then write your own code to convert to a dataframe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM