Remotely triggering a refresh in Tableau

A few years ago I wrote this code to trigger a refresh of a published data source (PDS) in Tableau.  I’ve adapted the code over that time and this is the latest version that I use in quite a few different places.

This script works for Tableau Server and Tableau Cloud because you need a Personal Access Token (PAT) to login.

You can use this for your own purposes and adapt as you need.  Here’s some tips on line numbers:

Line 6: create a PAT and paste the name here

Line 7: this is the secret part of the PAT

Line 9: Your Tableau Server, or your Tableau Cloud link

Line 10 : Your site name

These settings are pretty easy if you have Tableau Server or ask your Server Admin. If you use Tableau Cloud and your link is something like this:

https://prod-podname-a.online.tableau.com/t/acmecorp

that means your server_url is https://prod-podname-a.online.tableau.com/ and your site name is acmecorp

Line 11: The list of PDS names you want to refresh.

				
					import tableauserverclient as TSC

# Tableau settings
# My PAT as defined in Tableau Server

token_name = '**REMOVED**'
token_secret = '**REMOVED**'

server_url = 'https://MY_TABLEAU_SERVER.com/'
site = 'SITE_NAME'
pds_list = ['DATA_SOURCE_NAME']

server = TSC.Server(server_url, use_server_version=True)
# This is how you do it with a PAT
tableau_auth = TSC.PersonalAccessTokenAuth(token_name=token_name, personal_access_token=token_secret, site_id=site)
with server.auth.sign_in_with_personal_access_token(tableau_auth):
    print('[Logged in successfully to {}]'.format(server_url))

    all_datasources, pagination_item = server.datasources.get()

    for pds_name in pds_list:
        for datasource in all_datasources:
            if datasource.name == pds_name:
                refresh = server.datasources.get_by_id(datasource.id)

                # call the refresh method with the data source item
                refreshed_datasource = server.datasources.refresh(refresh)
                print('Refreshing ', datasource.name)

				
			

So that’s it .. pretty simple and I usually call this at the end of an ETL/ELT process.  Why wait for the time schedule in Tableau when you can refresh when you’re ready – as soon as the data has finished loading.

 

I’d love to know if this works for you and what are you using it for.  Either via email or reply to this post.  If you have any other ideas for using the Tableau REST APIs, please let me know.

But wait … what if you don’t have published data sources and you have packaged workbooks instead?  Well, I would always recommend published data sources over packaged workbooks, but if you really want to refresh the workbook then you can do this (thanks to Matt Wutt

				
					import tableauserverclient as TSC
 
# Tableau settings
# My PAT as defined in Tableau Server
 
token_name = '**REMOVED**'
token_secret = '**REMOVED**'

server_url = 'https://MY_TABLEAU_SERVER.com/'
site = 'SITE_NAME'
pds_list = ['WORKBOOK_NAME']
 
server = TSC.Server(server_url, use_server_version=True)

tableau_auth = TSC.PersonalAccessTokenAuth(token_name=token_name, personal_access_token=token_secret, site_id=site)
with server.auth.sign_in(tableau_auth):
    print('[Logged in successfully to {}]'.format(server_url))

    req1=TSC.RequestOptions(pagesize=1000)
    all_workbooks_items, pagination_item = server.workbooks.get(req1)
    for pds_name in pds_list:
        for workbook in all_workbooks_items:
            if workbook.name == pds_name:
                workbook = server.workbooks.refresh(workbook)
				
			

So you can see that there’s just a few changes from line 19 onwards, but essentially the logic is the same:

  • login with a PAT
  • find the object you want to refresh
  • call the refresh method

Leave a Reply

Your email address will not be published. Required fields are marked *