Automate Tableau Data Sources refresh using Python | by Cristian Saavedra Desmoineaux | Towards Data Science

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 not enough because you don't know when the population process is going to 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 are getting new files loaded into an Amazon S3 Bucket.
To solve these problems, I will explain how to refresh or update an existing Tableau Data Source and WorkBook using Python. I recommend checking this previous post to see other uses 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 you to use Miniconda. You can see more details in this post.
The first you will need is install the Tableau Server Client (TSC) using pip as recommends the documentation at https://tableau.github.io/server-client-python/docs/#install-tsc
pip install tableauserverclientpip install tableauserverclient --upgrade
We are going to use a Personal Access Token or PAT to connect. The next step is going to Tableau Server at the right-up corner and select My Account Settings.
Scroll down and copy the Token Secret of the Personal Access Token
To simplify, I created the class TableauServerConnection with the variables:
server_url, full HTTP address like https://10ax.online.tableau.com/
token_name, It 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')
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 following:
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)
In some cases, you have a WorkBooks connected to Data Sources and, at the same time, have his Data Source that needs to be refresh 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:
tableau.github.io
help.tableau.com
help.tableau.com
To finish, I am grateful to Carlos Lobera, who introduced me to Evan Slotnick, Anir Agarwal, and Elliott Stam, whos opened this door to automate the daily task for Tableau Server.