This Python code interacts with a Tableau server using the...
September 2, 2025 at 12:21 PM
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 athttps://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 fromauction_result_data
, which presumably is some external dataset (not defined in the provided code). - Filters such as:
Select Sec Type
,Select Auction Date
, andEnter CUSIP
are set usingpdf_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:
- Connects to a Tableau server using Personal Access Token authentication.
- Retrieves specific projects and workbooks.
- Filters and extracts views from a workbook (
Auction_Summary
). - Dynamically applies filters to views in Tableau, generates PDFs for the views, and saves them locally.
- 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