简体   繁体   中英

Querying Tableau Server for exporting a view using python and REST API

I am trying to export a tableau view as an image/csv (doesn't matter) using Python. I googled and found that REST API would help here, so I created a Personal Access Token and wrote the following command to connect: -

import tableauserverclient as TSC
from tableau_api_lib import TableauServerConnection
from tableau_api_lib.utils.querying import get_views_dataframe, get_view_data_dataframe

server_url = 'https://tableau.mariadb.com'
site = ''
mytoken_name = 'Marine'
mytoken_secret = '$32mcyTOkmjSFqKBeVKEZYpMUexseV197l2MuvRlwHghMacCOa'

server = TSC.Server(server_url, use_server_version=True)
tableau_auth = TSC.PersonalAccessTokenAuth(token_name=mytoken_name, personal_access_token=mytoken_secret, site_id=site)
with server.auth.sign_in_with_personal_access_token(tableau_auth):
    print('[Logged in successfully to {}]'.format(server_url))

It entered successfully and gave the message: -

[Logged in successfully to https://tableau.mariadb.com]

However, Iam at a loss now on how to access the tableau workbooks using Python. I searched here:-

https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_workbooks_and_views.htm

but was unable to write these commands like GET or others in python.

Can anyone help?

I'm assuming you don't know the view_id of the view you're looking for

Adding this after the print in the with block will query all the views available on your site;

 all_views, pagination_item = server.views.get()
 print([view.name for view in all_views])

Then find the view you're looking for in the printed output and note the view_id for use like this;

view_item = server.view.get_by_id('d79634e1-6063-4ec9-95ff-50acbf609ff5')

From there, you can get the image like this;

server.views.populate_image(view_item)
with open('./view_image.png', 'wb') as f:
    f.write(view_item.image)

The tableauserverclient-python docs should help you out a ton as well

https://tableau.github.io/server-client-python/docs/api-ref#views

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