简体   繁体   中英

Tableau embedded data source refresh using python

Is there a way to refresh Tableau embedded datasource using python. I am currently using Tableau server client library to refresh published datasources which is actually working fine. Can someone help me to figure out a way?

You can use "tableauserverclient" Python package. You can pip install it from PyPy. After installing it, you can consult the docs.

I will attach an example I used some time ago:

import tableauserverclient as TSC

tableau_auth = TSC.TableauAuth('user', 'pass', 'homepage')
server = TSC.Server('server')

with server.auth.sign_in(tableau_auth):
    all_datasources, pagination_item = server.datasources.get()
    print("\nThere are {} datasources on 
    site:".format(pagination_item.total_available))
    print([datasource.name for datasource in all_datasources])

The way you can reach them is kinda annoying from my perpective. You need to use populate_connections() function to load embedded datasources. It would be easier if you know the name of the workbook.

import tableauserverclient as TSC

#sign in using personal access token
server = TSC.Server(server_address='server_name', use_server_version=True)
server.auth.sign_in_with_personal_access_token(auth_req=TSC.PersonalAccessTokenAuth(token_name='tokenName', personal_access_token='tokenValue', site_id='site_name'))

#use RequestOptions() with a filter to pull an specific workbook
def get_workbook(name):
    req_opt = TSC.RequestOptions()
    req_opt.filter.add(TSC.Filter(req_opt.Field.Name, req_opt.Operator.Equals, name))
    return server.workbooks.get(req_opt)[0][0] #workbooks.get () function is intended to return a list items that you can iterate, but here we are assuming it will be find  only one result

workbook = get_workbook(name='workbook_name') #gets the workbook
server.workbooks.populate_connections(workbook) #this function will load all the embedded datasources in the workbook
for datasource in workbook.connections: #iterate in datasource list
    #Note: each element of this list is not an TSC.DatabaseItem, so, you will need to load a valid one using the "datasource_id" attribute from the element.
    #If you try server.datasources.refresh(datasource) it will fail
    ds = server.datasources.get_by_id(datasource.datasource_id) #loads a valid TSC.DatabaseItem
    server.datasources.refresh(ds) #finally, you will be able to refresh it
    ...

The best practice is do not embeddeding datasources but publish them independently.

Update: There is an easy way to achieve this. There are two types of extract tasks, Workbook and Data source. So, for embedded data sources, you need to perform a workbook refresh.

workbook = get_workbook(name='workbook_name')
server.workbooks.refresh(workbook.id)

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