Are you looking for a way to programmatically access and retrieve data from the Tableau Server? Python can be a powerful tool for automating this process using Tableau's REST API. In this blog, we will walk through the process of using Python to extract data from the Tableau Server, and provide a working example that demonstrates how to access information such as views, projects, and workbooks.
Connecting to Tableau Server and Authenticating
First, we'll need to set up the Python environment and install the necessary libraries. We will be using the tableauserverclient
library to interact with the Tableau Server through its REST API. Here's how to set up the initial authentication and connection to the Tableau Server:
import tableauserverclient as TSC
tableau_auth = TSC.TableauAuth('USERNAME', 'PASSWORD', site_id='')
server = TSC.Server('http://SERVER_URL', use_server_version=True)
server.auth.sign_in(tableau_auth)
Once authenticated, we can start accessing data from the Tableau Server. The following examples demonstrate how to retrieve information about views, projects, and workbooks.
Retrieving Views
In Tableau, views are individual visualizations or charts created within a Tableau workbook. These can include bar charts, line graphs, scatter plots, pie charts, maps, and other types of visual representations of data. Views are created by dragging and dropping fields from the data pane onto the view canvas and configuring the layout and properties. On Tableau Server, these views can be shared, accessed, and managed by users with appropriate permissions. Retrieving views through Tableau's REST API using Python refers to accessing information about these visualizations stored on the Tableau Server.
# view
views = []
for view in TSC.Pager(server.views):
views.append(view.name)
print(views)
Retrieving Projects
In Tableau, projects are used to organize and manage related content such as workbooks, data sources, views, and other resources. They enable the grouping of content and control access based on user permissions. Each project can have unique permissions, allowing for easier management and security of different types of content. Projects can also be nested to create a hierarchical structure for organizing content on the Tableau Server. Overall, projects provide a way to organize, manage, and control access to content within the Tableau Server.
Print the names of all projects
With the first code snippet, you could print the name of all projects by using the following code:
# project get
with server.auth.sign_in(tableau_auth):
# get all projects on site
all_project_items, pagination_item = server.projects.get()
print([proj.name for proj in all_project_items])
Create a DataFrame containing all attributes of Project
The project resources for Tableau are defined in the ProjectItem
class. The class corresponds to the project resources that you can access using the Tableau Server REST API. In addition to the name
, it also has content_permissions
, description
, id
, and parent_id
attributes. If you want to save all of them into a dataframe, you could use the following code:
# project get
with server.auth.sign_in(tableau_auth):
# get all projects on site
all_projects, pagination_item = server.projects.get()
df_projects = pd.DataFrame({
'name': [project.name for project in all_projects],
'content_permissions': [project.content_permissions for project in all_projects],
'description': [project.description for project in all_projects],
'id': [project.id for project in all_projects],
'parent_id': [project.parent_id for project in all_projects]
})
Retrieving Workbooks
The workbook resources for Tableau are defined in the WorkbookItem
class. The class corresponds to the workbook resources you can access using the Tableau REST API. Some workbook methods take an instance of the WorkbookItem
class as arguments. The workbook item specifies the project.
Print names of first 100 workbooks
With the first code snippet, you could print the name of all workbooks by using the following code:
with server.auth.sign_in(tableau_auth):
all_workbooks_items, pagination_item = server.workbooks.get()
# print names of first 100 workbooks
print([workbook.name for workbook in all_workbooks_items])
Retrieve all the workbooks and save them in a dataframe.
If the number of workbooks exceeds 100, you can use server.workbooks.all()
instead of server.workbooks.get()
to retrieve all workbooks. The following code snippet allows you to fetch all workbooks. In addition to the four attributes mentioned, you can refer to the WorkbookItem class for more information.
# workbook
with server.auth.sign_in(tableau_auth):
all_workbooks_items = server.workbooks.all()
df_workbook = pd.DataFrame({
'name': [workbook.name for workbook in all_workbooks_items],
'project_id': [workbook.project_id for workbook in all_workbooks_items],
'project_name': [workbook.project_name for workbook in all_workbooks_items],
'webpage_url': [workbook.webpage_url for workbook in all_workbooks_items]
})
Explore more
Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.
Top comments (0)