On Tableau you can run a schedule, but I'm looking to run a schedule via python/tabcmd or other method after our daily data operations are complete. This would allow us the ability to add and remove content to the schedule without having to code for that specific content. Currently each data source or workbook ID has to be manually added to the script.
You can refresh everything within a given schedule using the following Python code
import tableauserverclient as TSC
# Authenticate with personal access token
tableau_auth = TSC.PersonalAccessTokenAuth('token name', 'token value', 'site name')
server = TSC.Server('https://tableausiteurl.com/', use_server_version=True)
#name of schedule to find and run all task within
schedule_name = 'schedule to refresh'
# Use a context manager to ensure that you sign out of the server even if an error occurs
with server.auth.sign_in(tableau_auth):
try:
# Query schedules using the Tableau Server Client
all_schedules, pagination_item = server.schedules.get()
# Find the specified schedule
schedule2run = next((schedule for schedule in all_schedules if schedule.name == schedule_name), None)
if schedule2run:
# Get the list of all tasks on the server
all_tasks, pagination_item = server.tasks.get()
# Filter the tasks for those that are associated with the schedule to run
schedule2run_tasks = [task for task in all_tasks if task.schedule_id == schedule2run.id]
# Initialize a counter for successful refreshes
successful_refreshes = 0
# Run the refresh task for all associated content to that schedule
for task in schedule2run_tasks:
try:
server.tasks.run(task)
successful_refreshes += 1
except Exception as e:
print(f"Failed to trigger refresh for task with ID {task.id}: {e}")
print(f"{successful_refreshes} of {len(schedule2run_tasks)} tasks in the schedule '{schedule_name}' have been triggered to refresh.")
else:
print(f"Schedule '{schedule_name}' was not found.")
except Exception as e:
print(f"There was an error: {e}")