Using the Looker API to generate PDFs

sdk
api
python
pdf

(Joseph Axisa) #1

Downloading a PDF version of your dashboard is great and is very easy to do via the Looker UI. In this article, we make use of the Looker API (and the python SDK client) to do this.

Step 1:

Authenticate into the Looker API.

import looker

base_url = 'https://learn.looker.com:19999/api/3.0'
client_id = ''
client_secret = ''

# instantiate Auth API
unauthenticated_client = looker.ApiClient(base_url)
unauthenticated_authApi = looker.ApiAuthApi(unauthenticated=_client)

# authenticate client
token = unauthenticated_authApi.login(client_id=client_id, client_secret=client_secret)
client = looker.ApiClient(base_url, 'Authorization', 'token ' + token.access_token)

Step 2:

Create a new task to render the desired dashboard to a PDF using the create_dashboard_render_task endpoint. Please take note of the format of the dashboard_filters, which is a string and expects the filters in query URL format. e.g.: "My Filter=New York&My Other Filter=Brooklyn".

# instantiate render task API
renderTask = looker.RenderTaskApi(api_client=client)

height = 842
width = 595
output_format  = 'pdf'
dashboard_id = 241
body = {
         "dashboard_style": "tiled",
         "dashboard_filters": {
            "Created Date=12 months ago for 12 months"
         }
       }

# fire a render task and get its ID
task_response = renderTask.create_dashboard_render_task(dashboard_id, output_format, body, width, height)
task_id = task_response.id

###Step 3:
Use the render_task(id) endpoint to confirm the the render task has finished. Consequently, we can get the produced document using the render_task_results endpoint as follows:

# get the produced results
results = renderTask.render_task_results(task_id, _preload_content = False)
data = results.data

# write it to PDF
with open('output.pdf', 'wb+') as f:
    f.write(data)

PS: the _preload_content = false parameter is used to tell Python not to parse the body of the response as text.


Generating a PowerPoint presentation from all Looks in a Space
#2

I am not able to locate any Looker Python SDK which has a valid reference to looker.ApiClient.
Can you pass on the location where to download this this Looker Python SDK?

(edit)
Perhaps I misunderstood. Looks like this should be generated based off this article

I’m not all that familiar with swagger, but it’s confusing to me why this client would have to be generated. Especially considering all the dependencies required in order to get it done.

For now I’m going to attempt to interact with the API directly via http, instead.


(Danny Thorpe) #3

Hi evan,

As you surmised, we don’t currently have a ready-made Python client SDK package to access the Looker API available for download. You can generate a Python client SDK from the Looker API swagger metadata, as covered in the article you linked.

This does introduce some additional steps that you, our customers, have to go through to get started using the Looker API, and for that I apologize. The swagger codegen step does allow a wide variety of programming languages to access the Looker REST API without any additional effort from us.

Some folks who are comfortable making HTTP REST requests skip the whole SDK step and just write their code to make HTTP requests to the Looker API URLs directly. The downside to that is you don’t get the data types built for you, and you have to think in terms of HTTP requests all the time. The Swagger generated client-SDKs for the Looker API offer the convenience of accessing the Looker API like a function call library so you don’t have to get down and dirty with HTTP.

Providing pre-packaged Looker client SDK downloads is high on my personal wish list, but until I can find a couple of free weekends I’m not likely to make much progress on this front. :unamused:

If/when we do offer pre-packaged client SDKs for the Looker API, it will definitely be announced in an article here on this forum. Watch this space! :wink:

Sincerely,
-Danny


(Mike Ghen) #4

Can this be done with the Ruby SDK?


(Danny Thorpe) #5

@mikeghen1 Yes, the Looker client SDK for Ruby can download PDFs. https://rubygems.org/gems/looker-sdk

-Danny


(Mike Ghen) #6

Thanks, I found the gem already.

I don’t see any documentation on how to use the SDK to do that. Is there documentation for how to use that gem to do things other than manage users?


(vincent) #7

Hi @mikeghen1,

Here is our API doc on how to create a render task for a dashboard. But, I have played with this before and here is an example where I create a .png file for a look

#!/usr/bin/env ruby
require 'looker-sdk'

sdk = LookerSDK::Client.new(
  :client_id => ENV['API_ID'],
  :client_secret => ENV['API_KEY'],
  :api_endpoint => "https://self-signed.looker.com:19999/api/3.0",
  :connection_options => {:ssl => {:verify => false}}
)

png = sdk.create_look_render_task(164, "png", {}, {:query => {:width => 1000, :height => 1000}})

id = png[:id]

until sdk.render_task(id)[:status] == 'success' do
end

results = sdk.render_task_results(id)

File.open('look.png', 'w') { |file| file.write(results) }

As you can see, we first have to create a render task. Then, we have to wait until that task is completed which is what we do here:

until sdk.render_task(id)[:status] == 'success' do
end

Then, once it returns true, we get the results, store it in a variable, and save it to a file. Let me know if this helps! And of course, you will have to make some changes to your ruby script.

Cheers,
Vincent


Looker returns PNG/JPG as a String, which Charset?
API Visualizations with Filters
(Mike Ghen) #8

Thanks Vincent!


(Mike Ghen) #9

I followed the instructions to generate the PDF with Python and got this error:

Traceback (most recent call last):
  File "generate_pdf.py", line 38, in <module>
    results = renderTask.render_task_results(task_id, _preload_content=False)
  File "/Users/mikeghen/Documents/REDACTED/render_task_api.py", line 561, in render_task_results
    " to method render_task_results" % key
TypeError: Got an unexpected keyword argument '_preload_content' to method render_task_results

I generated my Python looker package using the instructions provided here: Generating Client SDKs for the Looker API. (Doing that was a pain :weary: I don’t understand why Looker only provides a Ruby SDK) I confirmed it worked with a few other API calls before I tried to do this PDF, render task thing.

When I just remove _preload_content I get:

Traceback (most recent call last):
  File "generate_pdf.py", line 46, in <module>
    data = results.data
AttributeError: 'str' object has no attribute 'data'

Looks like if I just pass results rather than results.data this works out OK without _preload_content:

results = renderTask.render_task_results(task_id)

# write it to PDF
with open('output.pdf', 'wb+') as f:
    f.write(results)