|
| 1 | +#### |
| 2 | +# This script demonstrates how to create extract tasks in Tableau Cloud |
| 3 | +# using the Tableau Server Client. |
| 4 | +# |
| 5 | +# To run the script, you must have installed Python 3.7 or later. |
| 6 | +#### |
| 7 | + |
| 8 | + |
| 9 | +import argparse |
| 10 | +import logging |
| 11 | + |
| 12 | +from datetime import time |
| 13 | + |
| 14 | +import tableauserverclient as TSC |
| 15 | + |
| 16 | + |
| 17 | +def main(): |
| 18 | + parser = argparse.ArgumentParser(description="Creates sample extract refresh task.") |
| 19 | + # Common options; please keep those in sync across all samples |
| 20 | + parser.add_argument("--server", "-s", help="server address") |
| 21 | + parser.add_argument("--site", "-S", help="site name") |
| 22 | + parser.add_argument("--token-name", "-p", help="name of the personal access token used to sign into the server") |
| 23 | + parser.add_argument("--token-value", "-v", help="value of the personal access token used to sign into the server") |
| 24 | + parser.add_argument( |
| 25 | + "--logging-level", |
| 26 | + "-l", |
| 27 | + choices=["debug", "info", "error"], |
| 28 | + default="error", |
| 29 | + help="desired logging level (set to error by default)", |
| 30 | + ) |
| 31 | + # Options specific to this sample: |
| 32 | + # This sample has no additional options, yet. If you add some, please add them here |
| 33 | + |
| 34 | + args = parser.parse_args() |
| 35 | + |
| 36 | + # Set logging level based on user input, or error by default |
| 37 | + logging_level = getattr(logging, args.logging_level.upper()) |
| 38 | + logging.basicConfig(level=logging_level) |
| 39 | + |
| 40 | + tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site) |
| 41 | + server = TSC.Server(args.server, use_server_version=False) |
| 42 | + server.add_http_options({"verify": False}) |
| 43 | + server.use_server_version() |
| 44 | + with server.auth.sign_in(tableau_auth): |
| 45 | + # Monthly Schedule |
| 46 | + # This schedule will run on the 15th of every month at 11:30PM |
| 47 | + monthly_interval = TSC.MonthlyInterval(start_time=time(23, 30), interval_value=15) |
| 48 | + monthly_schedule = TSC.ScheduleItem( |
| 49 | + None, |
| 50 | + None, |
| 51 | + None, |
| 52 | + None, |
| 53 | + monthly_interval, |
| 54 | + ) |
| 55 | + |
| 56 | + # Default to using first workbook found in server |
| 57 | + all_workbook_items, pagination_item = server.workbooks.get() |
| 58 | + my_workbook: TSC.WorkbookItem = all_workbook_items[0] |
| 59 | + |
| 60 | + target_item = TSC.Target( |
| 61 | + my_workbook.id, # the id of the workbook or datasource |
| 62 | + "workbook", # alternatively can be "datasource" |
| 63 | + ) |
| 64 | + |
| 65 | + extract_item = TSC.TaskItem( |
| 66 | + None, |
| 67 | + "FullRefresh", |
| 68 | + None, |
| 69 | + None, |
| 70 | + None, |
| 71 | + monthly_schedule, |
| 72 | + None, |
| 73 | + target_item, |
| 74 | + ) |
| 75 | + |
| 76 | + try: |
| 77 | + response = server.tasks.create(extract_item) |
| 78 | + print(response) |
| 79 | + except Exception as e: |
| 80 | + print(e) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main() |
0 commit comments