달나라 노트

Python tableauserverclient : Filtering 본문

Python/Python tableauserverclient

Python tableauserverclient : Filtering

CosmosProject 2020. 12. 21. 16:03
728x90
반응형

 

 

Tableau Server Client(TSC)

Let us check how to filter and get information I want.




Filtering workbook using workbook name and get information of that workbook.

# --------------------------------------------------
# Chapter 2. How to filter and get some information I want
# --------------------------------------------------

import tableauserverclient as TSC

def TSC_filter_workbook():
    tableau_server_url = 'tableau_url'  # e.g. https://tableau-server.test.com
    tableau_user_id = 'tableau_id'
    tableau_user_pw = 'tableau_password'

    server = TSC.Server(tableau_server_url)
    tableau_auth = TSC.TableauAuth(tableau_user_id, tableau_user_pw)

    print('Current REST API Version :', server.version)
    server.version = '2.8'  # Set version as 2.8
    print('Modified Rest API Version :', server.version, '\n')

    server.auth.sign_in(tableau_auth)  # You should log in first before doing something using tableau server client
    print('Logged in')

    req_options = TSC.RequestOptions()  # To get something from tableau using TSC, you should send RequestOption to TSC. This is to create RequestOptions class
    # add filter contents to request options.
    # Below filter contents is to make filter meaning extracting information satisfying this condition which is Field 'Name' is 'Equals' to 'A Dashboard'
    # 'A Dashboard' is one of workbook names we are using currently.
    req_options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
                                      TSC.RequestOptions.Operator.Equals,
                                      'A Dashboard'))

    filter_items, pagination_items = server.workbooks.get(req_options)
    # server.workbooks.get(req_options) has the following meaning
    # 'get' 'workbooks' from 'server' satisfying 'req_options' condition.
    # 'TSC' means the total library and 'server' means specific server I logged in. So request options should be applied to 'server' class not 'TSC'.
    # In returned 'filter_items', there will be some informations that I want to get such as 'Workbook name', 'Workbook id' etc.

    workbook_info = filter_items[0] # As filter_items is created as List like type. So I should take index = 0 value.

    workbook_name = workbook_info.name
    workbook_id = workbook_info.id
    workbook_project_name = workbook_info.project_name
    workbook_project_id = workbook_info.project_id
    workbook_created_at = workbook_info.created_at

    print('Workbook \'A Dashboard\'s workbook_name =', workbook_name)  # A Dashboard
    print('Workbook \'A Dashboard\'s workbook_id =', workbook_id)  # 95ad193f-g2ea-6132-b84d-b573523d0396
    print('Workbook \'A Dashboard\'s workbook_project_name =', workbook_project_name)  # Team_Apple
    print('Workbook \'A Dashboard\'s workbook_project_id =', workbook_project_id)  # 39bd297f-a2cb-8957-d82a-b936529g8495
    print('Workbook \'A Dashboard\'s workbook_created_at =', workbook_created_at, '\n')  # 2020-03-20 00:50:46+00:00

    # Project means upper directory containing workbooks.
    # Workbook 'A Dashboard' is included in project named 'Team_Apple'
    # And each workbook and project have workbook id and project id which are given by tableau server when project and workbook is created. (such as 95ad193f-g2ea-6132-b84d-b573523d0396)
    # View is in each workbook taken from tableau online.
    #
    # Example Directory Structure
    # Team_Apple (-> Project)
    # ├── A Dashboard (-> Workbook)
    # │   └── A Graph (-> View)
    # ├── B Dashboard (-> Workbook)
    # │   └── B History (-> View)
    # └── C (-> Workbook)
    #     ├── D_1 C (-> View)
    #     └── C History (-> View)

    server.auth.sign_out()
    print('Logged out')




- Output
Current REST API Version : 2.3
Modified Rest API Version : 2.8 

Logged in

Workbook 'A Dashboard's workbook_name = A Dashboard
Workbook 'A Dashboard's workbook_id = 95ad193f-g2ea-6132-b84d-b573523d0396
Workbook 'A Dashboard's workbook_project_name = Team_Apple
Workbook 'A Dashboard's workbook_project_id = 39bd297f-a2cb-8957-d82a-b936529g8495
Workbook 'A Dashboard's workbook_created_at = 2020-03-20 00:50:46+00:00 

Logged out




Filtering proejct using proejct name and get information of that proejct.

# --------------------------------------------------
# Chapter 2. How to filter and get some information I want
# --------------------------------------------------

import tableauserverclient as TSC

def TSC_filter_workbook():
    tableau_server_url = 'tableau_url' # e.g. https://tableau-server.test.com
    tableau_user_id = 'tableau_id'
    tableau_user_pw = 'tableau_password'

    server = TSC.Server(tableau_server_url)
    tableau_auth = TSC.TableauAuth(tableau_user_id, tableau_user_pw)

    print('Current REST API Version :', server.version)
    server.version = '2.8' # Set version as 2.8
    print('Modified Rest API Version :', server.version, '\n')

    server.auth.sign_in(tableau_auth)
    print('Logged in')

    req_options = TSC.RequestOptions()
    req_options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
                                      TSC.RequestOptions.Operator.Equals,
                                      'Team_Apple'))

    filter_items, pagination_items = server.projects.get(req_options) # You can apply request option(filter option) to not only workbook but also project using 'projects' attribute in 'server' class.
    # And 'get' method is used when you use request options filter.
    # But 'get_by_id' method is used when you use id itself such as '1a2a3b-d6s1f3h6-d5s4h6n2s4-d65s1s3'

    project_info = filter_items[0]

    project_name = project_info.name
    project_id = project_info.id
    # project_content_permissions = project_info.content_permissions
    # project_permissions = project_info.permissions
    project_parent_id = project_info.parent_id

    print('Project \'Team_Apple\'s project_name =', project_name)
    print('Project \'Team_Apple\'s project_id =', project_id)
    print('Project \'Team_Apple\'s project_parent_id =', project_parent_id, '\n')

    server.auth.sign_out()
    print('Logged out')


    # Example Directory Structure
    # Team_Apple (-> Project)
    # ├── A Dashboard (-> Workbook)
    # │   └── A Graph (-> View)
    # ├── B Dashboard (-> Workbook)
    # │   └── B History (-> View)
    # └── C (-> Workbook)
    #     ├── D_1 C (-> View)
    #     └── C History (-> View)




- Output
Current REST API Version : 2.3
Modified Rest API Version : 2.8 

Logged in

Project 'Team_Apple's project_name = Team_Apple
Project 'Team_Apple's project_id = 39bd297f-a2cb-8957-d82a-b936529g8495
Project 'Team_Apple's project_parent_id = 2a34f1ab-9g21-38c3-1284-7e9234aba4ba

Logged out




Filtering view using view name and get information of that view.

# --------------------------------------------------
# Chapter 2. How to filter and get some information I want
# --------------------------------------------------

import tableauserverclient as TSC

def TSC_filter_workbook():
    tableau_server_url = 'tableau_url' # e.g. https://tableau-server.test.com
    tableau_user_id = 'tableau_id'
    tableau_user_pw = 'tableau_password'

    server = TSC.Server(tableau_server_url)
    tableau_auth = TSC.TableauAuth(tableau_user_id, tableau_user_pw)

    print('Current REST API Version :', server.version)
    server.version = '2.8' # Set version as 2.8
    print('Modified Rest API Version :', server.version, '\n')

    server.auth.sign_in(tableau_auth)
    print('Logged in')

    req_options = TSC.RequestOptions()
    req_options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
                                      TSC.RequestOptions.Operator.Equals,
                                      'A Graph'))
    filter_items, pagination_items = server.views.get(req_options)

    view_items = filter_items[0]

    view_name = view_items.name
    view_id = view_items.id
    view_workbook_id = view_items.workbook_id
    view_project_id = view_items.project_id
    # view_csv = view_items.csv

    print('View name :', view_name)
    print('View id :', view_id)
    print('Workbook id of view \'A Graph\' :', view_workbook_id)
    print('Project id of view \'A Graph\' :', view_project_id, '\n')

    target_wb = server.workbooks.get_by_id(view_workbook_id)
    server.workbooks.populate_views(target_wb)

    server.auth.sign_out()
    print('Logged out')


    # Example Directory Structure
    # Team_Apple (-> Project)
    # ├── A Dashboard (-> Workbook)
    # │   └── A Graph (-> View)
    # ├── V Dashboard (-> Workbook)
    # │   └── V History (-> View)
    # └── C (-> Workbook)
    #     ├── D_1 C (-> View)
    #     └── C History (-> View)




- Output
Current REST API Version : 2.3
Modified Rest API Version : 2.8 

Logged in

View name : A Graph
View id : 2a3b6241-3749-1093-a5eg-b2da3a63bdg4
Workbook id of view 'A Graph' : 95ad193f-g2ea-6132-b84d-b573523d0396
Project id of view 'A Graph' : 39bd297f-a2cb-8957-d82a-b936529g8495

Logged out

Printed workbook id is the id of A Dashboard including 'A Graph'.

Printed project id is the id of Team_Apple including 'A Graph'.




Get workbook id from tag name

import tableauserverclient as TSC

def TSC_filter_workbook():
    tableau_server_url = 'tableau_url' # e.g. https://tableau-server.test.com
    tableau_user_id = 'tableau_id'
    tableau_user_pw = 'tableau_password'

    server = TSC.Server(tableau_server_url)
    tableau_auth = TSC.TableauAuth(tableau_user_id, tableau_user_pw)

    print('Current REST API Version :', server.version)
    server.version = '2.8' # Set version as 2.8
    print('Modified Rest API Version :', server.version, '\n')

    server.auth.sign_in(tableau_auth)
    print('Logged in')

    target_project_name = 'Team_Apple'
    tag_name = 'temp_tag'

    req_options = TSC.RequestOptions()
    req_options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Tags,
                                      TSC.RequestOptions.Operator.Equals,
                                      tag_name))
    filter_items, pagination_items = server.workbooks.get(req_options)

    for i in filter_items:
        wb_info = i
        wb_name = wb_info.name
        wb_id = wb_info.id
        print(wb_name)
        print(wb_id, '\n')

    server.auth.sign_out()
    print('Logged out')


    # Example Directory Structure
    # Team_Apple (-> Project)
    # ├── A Dashboard (-> Workbook (tag: temp_tag))
    # │   └── A Graph (-> View)
    # ├── B Dashboard (-> Workbook (tag: temp_tag))
    # │   └── B History (-> View)
    # └── C (-> Workbook)
    #     ├── D_1 C (-> View)
    #     └── C History (-> View)




- Output
Current REST API Version : 2.3
Modified Rest API Version : 2.8 

Logged in

A Dashboard
95ad193f-g2ea-6132-b84d-b573523d0396

B Dashboard
62da151f-e5aa-5127-d14g-c252731e5373

Logged out

When creating workbook, you can add tag in that workbook. So you can use the tag name as filter condition.

In the above example, there are 2 workbooks (A Dashboard, B Dashboard) with tag name 'temp_tag'. And you can get those 2 workbook's information using tag name.



 

 

 

 

 

728x90
반응형
Comments