호테의 노트에 오신 것을 환영합니다 🙌

Tableau와 Salesforce, Python과 SQL 등 데이터의 전반적인 것들을 다루는 기술 블로그입니다.

Tableau/DataDevQuest

[DataDevQuest] 이름으로 뷰 찾기

Hote's Note 2024. 11. 27. 01:11

이번 달부터 DataDevQuest를 도전해보려고 합니다.

https://datadevquest.com/find-a-view-by-name/

 

Find a View by Name - DataDevQuest

This challenge is designed to sharpen your skills in interacting with Tableau Server or Tableau Online programmatically.

datadevquest.com

 

첫번째 도전 과제는 '이름으로 뷰 찾기'입니다.

도전 개요

사용자로부터 입력을 받아 Tableau 뷰의 이름으로 해당 뷰에 대한 정보를 검색하고 가져오는 프로그램을 작성하세요. 이 프로그램은 Tableau Server 또는 Tableau Online에 연결하여 뷰를 검색하고 관련 세부 정보를 체계적인 형식으로 표시해야 합니다.

요구 사항

  1. 사용자 입력:
    • 사용자에게 찾으려는 Tableau 뷰의 이름을 입력하라는 메시지를 표시합니다.
  2. 입증:
    • Python 사용자:
      • 인증에는 Tableau Server 클라이언트(TSC) 라이브러리를 사용하세요 .
    • 다른 언어들:
      • Tableau REST API를 직접 사용하여 인증하세요.
    • 인증을 안전하고 효율적으로 처리합니다.
  3. 검색보기:
    • Tableau Server 또는 Tableau Online에서 사용자 입력과 일치하는 뷰를 검색합니다.
    • 검색은 대소문자를 구분하지 않아야 합니다 .
  4. 디스플레이 정보:
    • 뷰가 발견되면 다음 세부 정보를 표시합니다.
      • View Name
      • View ID
      • Workbook Name
      • Project Name
      • View URL
    • 여러 개의 뷰가 일치하는 경우 각 뷰에 대한 세부 정보를 표시합니다.
    • 조회수가 없는 경우, 사용자에게 해당 사실을 알립니다.

5. 오류 처리:

  • 인증 실패, 네트워크 문제, API 오류 등의 오류를 우아하게 처리합니다.
  • 사용자가 무엇이 잘못되었는지 이해하는 데 도움이 되는 의미 있는 메시지를 제공합니다.
  •  
import tableauserverclient as TSC
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

def main():
    # Step 1: Load Tableau Server details from environment variables
    tableau_server = os.getenv("TABLEAU_SERVER")
    tableau_username = os.getenv("TABLEAU_USERNAME")
    tableau_password = os.getenv("TABLEAU_PASSWORD")
    tableau_site = os.getenv("TABLEAU_SITE")
    view_name = input("Enter the name of the Tableau view to search for: ").strip()

    # Step 2: Tableau Server Authentication
    try:
        tableau_auth = TSC.TableauAuth(tableau_username, tableau_password, None)
        server = TSC.Server(tableau_server)
        server.use_server_version()
        print("Authenticating with Tableau Server...")

        with server.auth.sign_in(tableau_auth):
            print("Authentication successful!")

            # Step 3: Search for the view by name
            print(f"Searching for views with name containing '{view_name}'...")

            # Fetch all views
            all_views, pagination_item = server.views.get()
            matching_views = [
                view for view in all_views if view_name.lower() in view.name.lower()
            ]

            if matching_views:
                # Fetch all projects to match project names
                all_projects, _ = server.projects.get()

                print(f"Found {len(matching_views)} matching view(s):\n")
                for idx, view in enumerate(matching_views, start=1):
                    # Fetch related details about the workbook
                    workbook = server.workbooks.get_by_id(view.workbook_id)

                    # Match project name using the workbook's project ID
                    project_name = next(
                        (project.name for project in all_projects if project.id == workbook.project_id),
                        "Unknown Project"
                    )

                    # Construct the View URL
                    view_url = f"{tableau_server}/#/views/{view.content_url}"

                    print(f"{idx})")
                    print(f"- View Name: {view.name}")
                    print(f"- View ID: {view.id}")
                    print(f"- Workbook Name: {workbook.name}")
                    print(f"- Project Name: {project_name}")
                    print(f"- View URL: {view_url}")
                    print()
            else:
                print(f"No views found matching '{view_name}'.")

    except TSC.ServerResponseError as e:
        print(f"Server response error: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    main()

https://github.com/Heoquixote/DataDevQuest.git

 

GitHub - Heoquixote/DataDevQuest

Contribute to Heoquixote/DataDevQuest development by creating an account on GitHub.

github.com

 

코드에 대한 다양한 피드백 환영합니다 : )

도움이 되셨다면 공감 부탁드립니다

'Tableau > DataDevQuest' 카테고리의 다른 글

[DataDevQuest] 매개변수를 변경하여 PDF 생성  (1) 2025.02.04