Automate Tableau Data Sources refresh using Python
Suppose you want your Dashboards ready at the right moment when the data is available. In that case, you will find that using a Schedule Task is insufficient because you don’t know when the population process will finish!
The only missing piece is an automated process that refreshes the Dashboard after a trigger happens, like after the last step of an ETL is executed or when you get new files loaded into an Amazon S3 Bucket.
I will explain how to refresh or update an existing Tableau Data Source and WorkBook using Python to solve these problems. I recommend checking this previous post to see other use cases.
The following code shows you how to get the ID and execute Data Source or Workbook refresh.
If you don’t have Python installed, I recommend using Miniconda. You can see more details in this post.
Installing the REST API library for Python
The first you will need is to install the Tableau Server Client (TSC) using pip as recommended in the documentation at https://tableau.github.io/server-client-python/docs/#install-tsc
pip install tableauserverclient
pip install tableauserverclient --upgrade
Configuring Personal Access Token
We are going to use a Personal Access Token or PAT to connect. The next step is going to Tableau Server in the right-up corner and selecting My Account Settings.

Scroll down and copy the Token Secret of the Personal Access Token

If you don’t see the Personal Access Tokens option, you can try to activate using the link, replacing with your site:
https://10ax.online.tableau.com/?personalAccessTokensEnabled=true#/site/<site>/home
Connection
To simplify, I created the class TableauServerConnection with the variables:
- server_url, full HTTP address like https://10ax.online.tableau.com/
- token_name is the identification name of the Token made previously
- token_secret. It is the secret text of the Token created yet
- site. It **** is the site name. If you need to connect to the default, let empty.
Your connection will look like the following:
tsConn = TableauServerConnection('https://10ax.online.tableau.com/','APIToken','copyHereTheTokenSecret')

Data Source
The next step is to connect to the Tableau Server and refresh an existing Data Source. To do that, You will need the ID. Because it is not easy to find It, I created the function getDataSourcesTableauServer that lists all the Data Sources using the previous connection.
I am going to use Python Pandas to search the ID, creating a Data Frame with the list returned by **** the function:

Then you can get the ID of filtering the Data Frame as follows:

And then you can use refreshDataSourceTableauServer to refresh the Data Source by the ID like the following:

The next lines are the ones that do the magic. If you want to know more about what is doing, you can find information on the documentation page.
refresh datasource = server.datasources.get_by_id(datasource_id)
refreshed_datasource = server.datasources.refresh(datasource)
WorkBooks
In some cases, you have a WorkBooks connected to Data Sources and, at the same time, have his Data Source that needs to be refreshed too.
Like the previous step, the WorkBook required the ID to execute a refresh. To facilitate the search of the ID, use the function getWorkBooksTableauServer in the same way as the previous step.
To refresh the WorkBook, use the refreshDataSourceTableauServer function by the ID like the following:

Thanks
To finish, I am grateful to Carlos Lobera, who introduced me to Evan Slotnick, Anir Agarwal, and Elliott Stam, who opened this door to automate the daily task for Tableau Server.
Special thanks to Timothy Vermeiren and Francisco Pagliaricci for the workaround to activate the Personal Access Token using the URL.