Welcome to this month’s intermediate DataDevQuest challenge! This challenge is designed to sharpen your skills in interacting with Tableau Server or Tableau Online programmatically. Taking in more complicated input, transforming it into a format needed, and retrieving the REST API objects allows for other solutions to rapidly find and adjust settings on specific views, workbooks, and projects.
Challenge Overview
Objective
Write a program that takes input from the user to search and retrieve information about a Tableau view by its URL. The program should connect to Tableau Server or Tableau Online, search for the view, and display relevant details in an organized format.
Why This Challenge?
Working with Tableau’s APIs allows you to automate tasks, integrate Tableau with other systems, and extend its functionality. This challenge will give you hands-on experience with authentication, making API calls, and handling responses—essential skills for any Tableau developer.
Challenge Details
Requirements
- User Input:
- Prompt the user to enter the URL of a Tableau view they wish to find. The URL may be complete or incomplete. For the server and site you use as default, or another site for which you have credentials.
- Authentication:
- Python Users:
- Use the Tableau Server Client (TSC) library for authentication.
- Other Languages:
- Use the Tableau REST API directly for authentication.
- Handle authentication securely and efficiently.
- Python Users:
- View Search:
- Search for the view on Tableau Server or Tableau Online that matches the user’s input.
- Display Information:
- If the view is found, display the following details:
- View Name
- View ID
- Workbook Name
- Project Name
- View URL
- If multiple views match, display details for each.
- If no views are found, inform the user accordingly.
- If the view is found, display the following details:
- Error Handling:
- Gracefully handle errors such as authentication failures, network issues, or API errors.
- Provide meaningful messages to help the user understand what went wrong.
Constraints
- Programming Language:
- You may use any programming language.
- Python users are encouraged to use the Tableau Server Client (TSC) library.
- Users of other languages should utilize the Tableau REST API directly.
- Permissions:
- Assume the user has the necessary permissions to access the views on Tableau Server or Tableau Online.
- Code Quality:
- Write clean, maintainable code.
- Include comments to explain complex sections.
Optional Enhancements
- Other object types:
- What about other Tableau Objects?
- Workbooks
- Flows
- Projects
- Command-Line Arguments:
- Allow the user to pass the view name as a command-line argument.
- Export Filtered, High Resolution Image:
- Provide an option to download an image of the view.
Getting Started
For Python Users
- Tableau Server Client (TSC) Library:
- The TSC library simplifies interactions with the Tableau Server REST API.
- TSC GitHub Repository
- Install using pip:
pip install tableauserverclient
For Other Languages
- Tableau REST API Reference:
- Use the REST API to interact with Tableau Server programmatically.
- Tableau REST API Documentation
Sample Input/Output
Enter the name of the Tableau view to search for: Sales Dashboard
Searching for views...
Found the following view(s):
1)
- View Name: Sales Dashboard
- View ID: e8f2a1b4-1234-5678-9abc-def012345678
- Workbook Name: Annual Sales Reports
- Project Name: Corporate Reports
- View URL: https://your-tableau-server/views/AnnualSalesReports/SalesDashboard
2)
- View Name: Regional Sales Dashboard
- View ID: f9a3b2c5-2345-6789-0bcd-ef1234567890
- Workbook Name: Regional Reports
- Project Name: Sales Team
- View URL: https://your-tableau-server/views/RegionalReports/RegionalSalesDashboard
Process completed successfully.
Submission Guidelines
- Source Code:
- Publish your project publically in Github profile
- README:
- Include setup instructions.
- Describe how to run the program.
- Video of Solution:
- Include a video of your solution in the README file, which you can publish on Youtube and embed the iframe. Or save the video file in the directory root inside the repository.
- Comments:
- Ensure your code is well-commented.
- Submission:
- Submit your challenge in the following **forms**
Additional Resources
- Tableau Server Client (Python):
- Tableau REST API:
- Filtering and Sorting
- Authentication
- Community Support:
- Tableau Community Forums
- Stack Overflow (
tableau-apitag) - DataDev Slack Channel
Conclusion
This challenge is a great opportunity to delve into Tableau’s programmatic interfaces and enhance your automation skills. By completing this task, you’ll gain valuable experience that can be applied to a wide range of projects involving data visualization and analysis.
We look forward to seeing your innovative solutions! Happy coding!
Solution
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "tableauserverclient>=0.34",
# "python-dotenv",
# ]
# ///
import argparse
from collections.abc import Sequence
from dataclasses import dataclass
import os
import re
import sys
from typing import overload
from urllib.parse import urlparse
from dotenv import load_dotenv
import tableauserverclient as TSC
load_dotenv()
@dataclass
class SearchDetails:
server: str
site_url: str | None = None
content_url: str | None = None
auth: TSC.TableauAuth | TSC.PersonalAccessTokenAuth
@overload
def get_auth(
username: str, password: str, token_name: None, token_secret: None, site_url: str
) -> TSC.TableauAuth: ...
@overload
def get_auth(
username: None, password: None, token_name: str, token_secret: str, site_url: str
) -> TSC.PersonalAccessTokenAuth: ...
def get_auth(username, password, token_name, token_secret, site_url):
"""
From the provided credentials, return the object that will be used to
authenticate to Tableau Server or Tableau Cloud. Preference is given to
the provided credentials, but if they are not provided, the function will
attempt to read the credentials from the environment variables.
Parameters
----------
username: str | None
The username to use for authentication. Required if using username and
password authentication. Can be read from the TABLEAU_USERNAME
environment variable.
password: str | None
The password to use for authentication. Required if using username and
password authentication. Can be read from the TABLEAU_PASSWORD
environment variable.
token_name: str | None
The name of the personal access token to use for authentication. Required
