Tableau Server is a product from Tableau that allows you to use the functionality of Tableau without having to download and open workbooks in Tableau desktop.  It works like every other server that lets you store things safely and also provides a collaborative environment for working. Tableau users publish data sources to the server when they want to share data connections they’ve defined. It is important to know how to get Tableau Server user details and data sources from tons of data sources.

Tableau Server Client(TSC) is a Python library for the Tableau Server REST API. You can read about TSC here. There are instances where a lot of Tableau server users; let’s say thousands publish a huge number of data sources. You will agree that it will look disorganized when you don’t know who published a particular data source or which data source a user published.

 It is important to know that on the server, there is always a unique identifier(id) for each data source. Also, there is an owner identifier (owner_id) of the data source. The owner identifier(owner_id) indicates the user that publishes on the server which also corresponds to the user identifier(user_id).

 I assume you have Python 3 installed if not you can download anaconda which comes with Python packages. In this tutorial, you will learn how to get Tableau server user details and data sources using Tableau Server Client. Let’s get in.

Step 1: Connect to the Tableau Server using the Tableau Server Client(TSC) library.

Input these lines of code which includes the username and password to login to the server.

import tableauserverclient as TSC  
import pandas as pd 
tableau_auth = TSC.TableauAuth('Username', 'Password')

This line of code includes the connection link to the server and login.

server = TSC.Server('http://3.227.165.186')  
request_options = TSC.RequestOptions(pagesize=1000) 
  
with server.auth.sign_in(tableau_auth):   

Step 2: Get the list of all the users.

Input this line of code to fetch the list of all the users on the Tableau server. Then input the user id and the user name of the Tableau server in a dictionary, a key value pair.

 all_users = list(TSC.Pager(server.users, request_options))    
userinfo = [{user.id:user.name} for user in all_users]  

Step 3: Get the list of all the data sources.

Now, we will get the list of all the data sources and all the unique data source identifiers on the Tableau server. Input these lines of code

    all_datasources = list(TSC.Pager(server.datasources, request_options)) 
    datasource_ids = [datasource.id for datasource in all_datasources]  

Step 4: Use Pandas library to get the user id, the user name, the data source id, and the data source owner id in a data frame.

Placing the user information into a Pandas Data Frame is a good way to interact with the list and perform other operations.

(a)Input the code below as a template for getting user details into a Pandas Data Frame and sign out afterward.

 datasource_owner_id = [{datasource.name:datasource.owner_id} for datasource in all_datasources] 
 print('The datasource name and the consecutive datasource owner_id for the default site is as
 follows:',datasource_owner_id) 
server.auth.sign_out() 

The next part of the code will get the user id and the name of the server users. It will then turn it to columns of a data frame.

Now, we can open a list for key1, key22, value1, and value22. Then append the dictionary values and keys to the list. Append key2 to key22 empty list and value2 to the value22 empty list

for userdiction in userinfo: 
    for key2, value2 in userdiction.items(): 
        key22.append(key2)  
        value22.append(value2) 
df = pd.DataFrame({"datasource_owner_id":key22,"Name":value22}) 

(b) Make a loop to go through the dictionary in the data source_owner_id.

Get the key and the value of the Dictionary “data”  and append the key to the key1 empty list. Then, append the value to the value1 empty list.

key1 = [] 
value1 = [] 
for data in data source_ owner_id: 
    for key, value in data.items():
        key1.append(key) 
        value1.append(value)  
df2 = pd.DataFrame({"datasource_name":key1,"datasource_owner_id":value1})

Note that at this point there are two different data frames. The first one is the df and the other is the df2. The df contains the list of all ids of the users of the server and the name of all the users of the server.  

Step 5: Map the users of the server to all the data sources that they published.

The next line of code joins the two data frames and merges the two data sets with the datasource_owner_id column.

result = pd.merge(df, 
df2[['datasource_name','datasource_owner_id']], on='datasource_owner_id') 
 

Step 6: Finally, save the data frame to a CSV file.

print(result) 
result.to_csv

The result of the joined table will look like the image below. 

If you found this blog post helpful, kindly share it on social platforms with the buttons below. Questions are welcomed in the comments section.

Call Now Button