Search code examples
pythonsdklooker

How can I get a raw Query from Looker SDK?


I am facing some problems while trying to use Looker SDK with Python.

Following the steps from this tutorial https://colab.research.google.com/github/looker-open-source/sdk-codegen/blob/main/python/python-sdk-tutorial.ipynb I just cannot execute the step 2.

try:
  response = sdk.run_look(
    look_id=look.id,
    result_format= "json" # Options here are csv, json, json_detail, txt, html, md, xlsx, sql (returns the raw query), png, jpg. JSON is the easiest to work with in python, so we return it.
  )
  data = json.loads(response) #The response is just a string, so we have to use the json library to load it as a json dict.
  print(data) #If our query was successful we should see an array of rows.
except:
  raise Exception(f'Error running look {look.id}')

When I try to run this code I get the error

Exception: Error running look 1234

The same occurs when I try with run_query() method. Does anyone know what it can be?

 try:
  response = sdk.run_query(
    query_id=explore_query.id,
    result_format= "sql" # Options here are csv, json, json_detail, txt, html, md, xlsx, sql (returns the raw query), png, jpg. JSON is the easiest to work with in python, so we return it.
  )
  print(response) #If our query was successful we should see an array of rows.
except:
  raise Exception(f'Error running query {explore_query.id}')

Solution

  • Problem solved. The issue was happening because the SDK by default it setted to production when the dashboards only existed on dev.

    The following line was executed to set the sdk to dev:

    sdk.update_session(body=models.WriteApiSession(workspace_id="dev"))
    

    This following one is to set to a especific branch:

    sdk.update_git_branch(project_id="lookerhub", body=mdls.WriteGitBranch(name="branch_name"))
    

    Where project_id is the project you are using and name is the branch name you are appointing to.

    With this steps followed, the problem is solved and everything runs well.