달나라 노트

Python tableauserverclient : populate 본문

Python/Python tableauserverclient

Python tableauserverclient : populate

CosmosProject 2022. 5. 25. 21:48
728x90
반응형

 

 

 

Project, Workbook, View 객체를 얻어와서 attribute를 조회할 때 일반적인 방법으로는 조회가 되지 않는 attribute가 있습니다.

 

아래 코드를 보시죠.

 

import tableauserverclient as TSC

id = 'user_1'
pw = 'pw_1'
url_tableau = 'https://tableau-server.test.com'

server = TSC.Server(url_tableau)
tableau_auth = TSC.TableauAuth(id, pw)


server.auth.sign_in(tableau_auth)


req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
                                 TSC.RequestOptions.Operator.Equals,
                                 'View 1'))

list_view_obj, pagination_item = server.views.get(req_option)
print(list_view_obj)

view_obj = list_view_obj[0]

print('View id =', view_obj.id)
print('View name =', view_obj.name)
pdf_info = view_obj.pdf


server.auth.sign_out()



-- Result
View id = 11vv11vv11-1111-1111-1111-1111vvvv0001
View name = View 1
Error: View item must be populated with its pdf first.

 

위 코드는 이름이 View 1인 View 객체를 불러오고 View ID와 View Name그리고 View의 pdf 정보를 출력하는 코드입니다.

 

View 객체에는 pdf attribute가 있습니다. pdf attribute는 View를 PDF의 형태로 만들기 위해 필요한 정보를 가지고 있습니다.

View attributes = https://tableau.github.io/server-client-python/docs/api-ref#views

 

- pdf_info = view_obj.pdf

근데 위 부분에서 에러가 발생합니다.

 

- Error: View item must be populated with its pdf first.

에러메세지를 보면 위와 같은데 간단하게 해석해보면 pdf attribute를 사용하려면 View 객체가 먼저 populate 되어야한다는 의미입니다.

 

 

 

populate이라는 단어의 의미를 찾아보면 인구/살다/거주하다 라는 의미가 있습니다.

근데 잘 찾아보면 다른 의미가 또 있습니다.

 

COMPUTING specialized
to automatically add information to a list or table on a computer:
- There are several ways to populate the database.

(사전 출처 = https://dictionary.cambridge.org/ko/%EC%82%AC%EC%A0%84/%EC%98%81%EC%96%B4/populate)

 

Computing 영역에서 populate이라는 단어가 사용되면 어떤 뜻으로 사용되는지에 대한 내용입니다.

해석해보면 list나 table에 정보를 채워넣는 것이라고 합니다.

 

즉, Computing 영역에서 populate이라는 단어의 의미는 데이터를 채워넣는 동작을 의미한다고 생각하면 됩니다.

 

 

 

- Error: View item must be populated with its pdf first.

이제 다시 에러메세지를 봅시다.

해석해보면 View item은 pdf로 데이터가 채워져야한다 라는 의미입니다.

여기서 말하는 View item은 get method로 얻어온 View 객체를 의미합니다.

 

Tableau에서는 객체 또는 class라는 개념을 item이라고 표현하는 것 같더군요.

Proejct item = Proejct 객체 = Proejct class

Workbook item = Workbook 객체 = Workbook class

View item = View 객체 = View class

 

어쨌든 View 객체에 먼저 pdf 데이터를 채워넣으라는 의미입니다.

 

즉, pdf attribute를 얻어오려면 먼저 View 객체에 pdf 정보를 채워넣어야 합니다.

 

View 객체 생성 -> View 객체에 pdf 정보를 채워넣음(=populate) -> pdf attribute 조회

위같은 방식으로 pdf attribute를 조회할 수 있습니다.

 

 

공식 문서에서는 아래와 같이 설명하고 있습니다.

(출처 = https://tableau.github.io/server-client-python/docs/api-ref#views)

 

 

The PDF of the view. You must first call the views.populate_pdf method to access the PDF content.

설명을 보면 pdf content에 접근하기 위해선 views.populate_pdf method를 이용하라고하네요.

 

 

그러면 코드를 수정해봅시다.

 

import tableauserverclient as TSC

id = 'user_1'
pw = 'pw_1'
url_tableau = 'https://tableau-server.test.com'

server = TSC.Server(url_tableau)
tableau_auth = TSC.TableauAuth(id, pw)


server.auth.sign_in(tableau_auth)


req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
                                 TSC.RequestOptions.Operator.Equals,
                                 'View 1'))

list_view_obj, pagination_item = server.views.get(req_option)
print(list_view_obj)

view_obj = list_view_obj[0]

print('View id =', view_obj.id)
print('View name =', view_obj.name)


server.views.populate_pdf(view_obj)
with open(view.pdf, 'wb') as f:
    f.write(view_obj.pdf)


server.auth.sign_out()



-- Result
View id = 11vv11vv11-1111-1111-1111-1111vvvv0001
View name = View 1

 

pdf 정보를 제대로 이용할 수 있도록 코드를 고쳐보았습니다.

 

 

server.views.populate_pdf(view_obj)
with open(view.pdf, 'wb') as f:
    f.write(view_obj.pdf)

 

중요한 것은 이 부분입니다.

 

- server.views.populate_pdf(view_obj)

populate_pdf method를 이용해서 view_obj 객체의 pdf attribute에 View의 pdf 정보를 채워넣었습니다.

 

그리고 open ~ write 구문으로 view_obj.pdf attribute에 채워진 pdf 정보를 담은 pdf 파일을 생성하였습니다.

 

위 코드를 실행하면 view.pdf 라는 파일이 생성될 것이고, pdf 파일을 열면 Tableau web에서 볼 수 있는 대쉬보드가 pdf로 생성되어있을 것입니다.

 

 

 

 

 

 

 

 

 

import tableauserverclient as TSC

id = 'user_1'
pw = 'pw_1'
url_tableau = 'https://tableau-server.test.com'

server = TSC.Server(url_tableau)
tableau_auth = TSC.TableauAuth(id, pw)


server.auth.sign_in(tableau_auth)


req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
                                 TSC.RequestOptions.Operator.Equals,
                                 'View 1'))

list_view_obj, pagination_item = server.views.get(req_option)
print(list_view_obj)

view_obj = list_view_obj[0]

print('View id =', view_obj.id)
print('View name =', view_obj.name)


server.views.populate_csv(view_obj)
with open(view.csv, 'wb') as f:
    f.write(b''.join(view_obj.csv))


server.auth.sign_out()



-- Result
View id = 11vv11vv11-1111-1111-1111-1111vvvv0001
View name = View 1

 

pdf와 거의 동일한 맥락으로 View의 csv도 생성할 수 있습니다.

 

 

 

 

server.views.populate_csv(view_obj)
with open(view.csv, 'wb') as f:
    f.write(b''.join(view_obj.csv))

 

- server.views.populate_csv(view_obj)

populate_csv method를 이용해서 View 객체의 csv attribute에 View의 csv 정보를 채워넣습니다.

pdf와 방식은 동일하지만 populate_csv method를 사용한다는 점이 다르네요.

 

그리고 open ~ write 구문을 이용해서 마찬가지로 view.csv 파일을 생성합니다.

그러면 View에 사용된 데이터가 csv 파일로 저장될 것입니다.

 

pdf와 csv의 좀 다른 점은 pdf는 View의 모양을 그대로 pdf로 만들어준다면

csv는 View에서 표시되고있는 데이터를 마치 DataBase에 있는 세로데이터 형식으로 변환해서 나타내준다는 것입니다.

 

참고로 f.write(b''.join(view_obj.csv)) 처럼 적은 이유는 csv를 만들기 위해선 bytes 데이터가 필요하기 때문입니다.

그래서 앞에 b를 붙여 byte 데이터로 변환해준겁니다.

 

 

 

 

 

 

 

View의 csv attribute에 대한 설명도 tableauserverclient 공식 문서를 보면 아래와 같이 poulate_csv를 사용하라고 나와있습니다.

(출처 = https://tableau.github.io/server-client-python/docs/api-ref#views)

 

 

 

 

여기서는 대표적으로 pdf와 csv를 알아봤는데 이 2개의 attribute 외에도 populate 단계를 거쳐야 하는 attribute가 많습니다.

 

어떤 attribute이건 populate를 진행하는 방식은 거의 동일하니 위 내용을 참고삼아 진행하면 됩니다.

 

 

 

 

 

 

이렇게 일반적으로 view_obj.id / view_obj.name 처럼 attribute를 조회하지 못하고 굳이 populate이라는 단계를 거치게 해놓은 이유는 저도 정확히는 모르겠으나 아마 성능 또는 크기 때문이 아닐까 싶습니다.

 

id, name같은 attribute는 사실 그렇게 데이터의 크기가 크지 않을겁니다.

하지만 pdf 정보같은 경우 View에 포함된 모든 숫자와 서식 등에 대한 정보가 다 담겨야하니 크기가 크겠지요.

또한 tableauserverclient를 사용하면서 pdf 정보를 항상 사용해야하는 것도 아닐겁니다.

즉, pdf 정보는 크기는 큰데 그렇게 자주 사용되지는 않는 정보로 판단되는 것이죠.

 

따라서 평소에는 pdf attribute를 비워두다가 pdf 정보가 필요해지면 populate method로 pdf정보를 View 객체에 채워준 후 pdf 정보를 얻을 수 있도록 한게 아닐까 싶네요.

 

 

 

 

 

 

728x90
반응형
Comments