This Python code interacts with a Tableau server using the...

September 2, 2025 at 12:21 PM

token_name = input("Please Enter your tableau token name \n") # token_name = "tdftableaub1dxt14" secret_token = input("Please Enter your tableau secret token \n") # secret_token = "tQfL/oLgQ2GjxsxIFTY8Eg==:4GBYPK1bG1UcsJNxxjf2W6zCLR3Yxk2I" tableau_auth = TSC.PersonalAccessTokenAuth(token_name, secret_token, site_id='2B-TreasuryAuction') server = TSC.Server('https://tableau.frb.org', use_server_version=True, http_options={"verify": False}) with server.auth.sign_in(tableau_auth): print("Connected") projects, pagination_item = server.projects.get() all_projects_dict = {} for proj in projects: all_projects_dict[proj.name] = proj.id workbooks = [wb for wb in TSC.Pager(server.workbooks) if wb.project_id == all_projects_dict['TDF PROD']] all_workbooks_dict = {} for wb in workbooks: if wb.name.startswith('Auction_Summary'): all_workbooks_dict[wb.name] = wb.id auction_summary = server.workbooks.get_by_id(all_workbooks_dict[wb.name]) views = [view for view in auction_summary.views] print(auction_result_data) for security in range(len(auction_result_data['security'])): file_name = auction_result_data['file_name'][security] pdf_req_option = TSC.PDFRequestOptions(page_type=TSC.PDFRequestOptions.PageType.Letter, orientation=TSC.PDFRequestOptions.Orientation.Portrait, maxage=1) print(auction_result_data['security'][security], auction_result_data['auction_date'][security], auction_result_data['cusip'][security]) pdf_req_option.vf('Select Sec Type', auction_result_data['security'][security]) pdf_req_option.vf('Select Auction Date', auction_result_data['auction_date'][security]) pdf_req_option.vf('Enter CUSIP', auction_result_data['cusip'][security]) all_views_dict = {} sheet_idx = 10 for view in views: sheet_idx += 1 all_views_dict[view.name] = view.id server.views.populate_pdf(view,pdf_req_option) print(view.name) with open(path_root + "\pdfs\\" + str(sheet_idx) + " " + view.name + '.pdf', 'wb') as f: f.write(view.pdf) pdfFiles = [] # variable # for loop for all files with .pdf in the name. for root, dirs, filenames in os.walk(path_root + "\\pdfs"): # Root and directory pathway. for filename in filenames: if filename.lower().endswith('.pdf'): pdfFiles.append(os.path.join(root,filename)) # Remove Report filter for file in pdfFiles: if "Report Filter" in file: pdfFiles.remove(file)

This Python code interacts with a Tableau server using the tableauserverclient (TSC) library to extract data, apply view filters, and download PDFs specific to certain views. Let's break down what it does step-by-step:


1. User Input for Tableau Authentication

  • Prompts the user to input:
    • token_name: The name of a Tableau Personal Access Token.
    • secret_token: The associated secret key/token.
  • These credentials are used to authenticate to Tableau Server.

2. Authenticate and Connect to Tableau

  • Uses TSC.PersonalAccessTokenAuth to authenticate to a Tableau server located at https://tableau.frb.org.
  • Specifically connects to the site '2B-TreasuryAuction'.
tableau_auth = TSC.PersonalAccessTokenAuth(token_name, secret_token, site_id='2B-TreasuryAuction')
with server.auth.sign_in(tableau_auth):

3. Retrieve Projects

  • Fetches all projects on the Tableau server into the projects collection.
  • Creates a dictionary, all_projects_dict, mapping project names to project IDs.
projects, pagination_item = server.projects.get()

4. Filter for Workbooks in the TDF PROD Project

  • Filters all workbooks within the TDF PROD project (using this project's ID from the earlier dictionary).
  • Focuses on workbooks whose names start with Auction_Summary.
  • Creates a dictionary, all_workbooks_dict, containing the names and IDs of applicable workbooks.
workbooks = [wb for wb in TSC.Pager(server.workbooks) if wb.project_id == all_projects_dict['TDF PROD']]

5. Retrieve Views and Apply Filters

  • Retrieves the views (dashboard/sheet visualizations) for a specific workbook (auction_summary).
  • Dynamically applies view filters (vf) based on data from auction_result_data, which presumably is some external dataset (not defined in the provided code).
  • Filters such as:
    • Select Sec Type,
    • Select Auction Date, and
    • Enter CUSIP are set using pdf_req_option.vf() before generating PDFs.

6. Generate PDFs for Views

  • Iterates through the views and generates a PDF for each one filtered by the options above.
  • The PDFs are saved to a folder (path_root + "\pdfs\") using a structured file naming convention.
server.views.populate_pdf(view, pdf_req_option)

7. Aggregate PDFs in a Directory

  • Walks through the path_root + "\pdfs\" directory to collect all .pdf files.
  • Filters out any PDF files containing "Report Filter" in their filenames.
for root, dirs, filenames in os.walk(path_root + "\\pdfs"):

Summary

This script:

  1. Connects to a Tableau server using Personal Access Token authentication.
  2. Retrieves specific projects and workbooks.
  3. Filters and extracts views from a workbook (Auction_Summary).
  4. Dynamically applies filters to views in Tableau, generates PDFs for the views, and saves them locally.
  5. Aggregates all PDFs into a local directory while excluding certain unwanted reports (e.g., containing "Report Filter").

Dependencies

  • Python 3.x.
  • Tableau Server Client (tableauserverclient library).
  • External dependencies like os for file/directory handling.
  • The variable auction_result_data, which is assumed to be a dictionary-like structure containing required filtering data (security, auction_date, cusip, etc.).
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node