Introduction
In most cases, Tableau tasks are scheduled to run at designated times, however, there may be occasions where the flexibility is not sufficient. If you're looking for a more flexible way to work with Tableau, you might want to check out the Tableau Server Client. Itβs a Python library for the Tableau Server REST API. Using the TSC library, you can manage and change many of the Tableau Server and Tableau Cloud resources programmatically.
Step 1: Authentication
The provided Python code establishes a connection to Tableau Server using the Tableau Server Client (TSC) library. It authenticates using the provided account credentials and establishes a connection to the specified Tableau Server site. Once authenticated, the code allows access to Tableau Server resources for tasks such as querying data, publishing workbooks, and managing server content.
import pandas as pd
import tableauserverclient as TSC
tableau_auth = TSC.TableauAuth('account', 'password', site_id='site_id')
server = TSC.Server('http://localhost', use_server_version=True)
server.auth.sign_in(tableau_auth)
Make sure to replace 'account
', 'password
', and 'site_id
' with actual credentials before running the code.
Step 2: List All Tasks and Find Task ID
This code signs in to Tableau Server using provided authentication credentials, then retrieves all tasks from the server and creates a dataframe to store the Taskid
, schedule_item
, and task_type
for each task.
# get all tasks and find the Taskid
with server.auth.sign_in(tableau_auth):
all_tasks, pagination_item = server.tasks.get()
df_tasks = pd.DataFrame()
df_tasks['Taskid'] = [task.id for task in all_tasks]
df_tasks['schedule_item'] = [task.schedule_item for task in all_tasks]
df_tasks['task_type'] = [task.task_type for task in all_tasks]
print(df_tasks)
You can easily navigate through the dataframe to locate the specific task you wish to work with and then proceed to copy its corresponding Taskid in preparation for the next step.
Step 3: Run or Delete a Task
run a task
You could use the following code to runs the specified extract refresh task, make sure to replace the id
with the actual id that you want to work with.
# replace the id
with server.auth.sign_in(tableau_auth):
task = server.tasks.get_by_id('9f9e9d9c-8b8a-8f8e-7d7c-7b7a6f6d6e6d')
server.tasks.run(task)
print("successfully refreshed")
delete a task
Deleting a task may not be as frequently utilized as running one, but it is just as straightforward. With the following code, you can smoothly delete a task.
# delete a task
with server.auth.sign_in(tableau_auth):
server.tasks.delete('9f9e9d9c-8b8a-8f8e-7d7c-7b7a6f6d6e6d')
print("successfully deleted")
Tips
the Taskid
is not stets, it will change. So if you want to run a specific job, you could use the following code to get the latest Taskid
every time.
def get_latest_taskid(schedule_name):
with server.auth.sign_in(tableau_auth):
all_tasks, pagination_item = server.tasks.get()
for task in all_tasks:
if schedule_name in str(task.schedule_item):
return task.id
References
Tableau Server Client (Python)
Explore more
Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.
Top comments (0)