The Civis API supports Tableau workbook extract refreshes, ensuring that your reports are always backed by the most up-to-date data.
Refreshes can be triggered with the post_refresh endpoint in the Python and R clients, which you can access from Python or R scripts in Platform. This will also allow you to schedule your script or include it as a task in a workflow. For example, you might have a workflow that ingests and transforms data that is ultimately fed to a report -- you can add an API refresh at the end of this workflow to ensure the report gets updated as the data does. To check whether or not the refresh has succeeded, you can use the get_reports_id_refresh_job_id endpoint.
Triggering an extract refresh via the API only requires the ID of the report in Platform (note that this ID is different from the workbook ID in Tableau Server). You must have editor permission on the report object in order to trigger an extract refresh. Examples using both the Python and R API Clients are shown below. General API documentation is here.
import civis
client = civis.APIClient()
refresh_response = client.reports.post_refresh(123456) #provide the id of the report
print(refresh_response)
client.reports.get_refresh(123456, refresh_response.tableau_refresh_job_id)If you’d like to poll for refresh status, you can do so manually:
import time
while True:
resp = client.reports.get_refresh(123456, refresh_response.tableau_refresh_job_id)
if resp.status in ['queued', 'running']:
print(resp.status) # optional status tracking
time.sleep(5) # seconds, increase if your refresh jobs take a long time to run
else:
print(f'Job {resp.job_id} {resp.status}')
if resp.error:
print(f'Error: {resp.error}')
break
Or using CivisFutures:
refresh_response = client.reports.post_refresh(123456)
future = civis.futures.CivisFuture(client.reports.get_refresh, (126952, refresh_response.tableau_refresh_job_id))
future.result()If you have long-running refresh jobs and would like to know when the refresh completes while minimizing compute usage, another polling option is to include a status check in a small python job that will error if the job has not completed, then include that job in a workflow with retries. See workflows-public for an example.
To refresh multiple reports, use:
import civis
client = civis.APIClient()
#use a loop to refresh multiple reports
id_list = [53104, 98765, 34567]
for id in id_list:
refresh = client.reports.post_refresh(id)
print(refresh)In the R client, you can use the following:
library (civis)
refresh_response <- reports_post_refresh(53104)
print(refresh_response)
reports_get_refresh(53104, refresh_response$tableauRefreshJobId)For multiple reports, use:
library (civis)
id_list <- c(53104, 98765, 34567)
for (id in id_list) {
refresh <- reports_post_refresh(id)
print(refresh)
}
Comments
0 comments
Article is closed for comments.