簡體   English   中英

使用 python 的 tableau 服務器客戶端返回所有可能的過濾器值

[英]Return all possible filter values with tableau server client for python

I am using the Tableau Server REST API via the python server client library ( https://github.com/tableau/server-client-python ) to automatically download pdf versions of several tableau workbook views.

對於每個 pdf 導出,我正在根據設置的參數值過濾 Tableau 視圖。 目前,所有可能的參數值都是手動指定的,但我想找到所有可能的選項並遍歷它們(為每個過濾器選項生成一個 pdf)。 那可能嗎?

一個小示例代碼,其中根據參數 country 和可能的值“Sweden”和“Norway”過濾了 tableau 視圖:

import tableauserverclient as TSC 

Parameters = [{"Parameter Name" : "Country", "ParameterValue" : "Sweden"}, 
              {"Parameter Name" : "Country", "ParameterValue" : "Norway"}]

for params in Parameters:
      image_req_options = TSC.PDFRequestOptions()
      image_req_options.vf(params["ParameterName"], params["ParameterValue"]

      server.views.populate_pdf(view, image_req_options)
      with open('output_pdf_{}.pdf'.format(params["ParameterValue"]), 'wb') as f:
           f.write(view.pdf)

我不想定義字典的參數列表,而是想為參數國家找到所有可能的 ParameterValues。

您可以通過使用相同的已發布數據源和基礎過濾器創建單獨的私有工作簿來實現此目的,然后使用 ViewItem class 方法populate_csv返回所需的值。 您將無法返回硬編碼參數,但您可以使用從維度字段加載的動態參數 在此處了解有關動態參數的更多信息。

populate_csv function 返回與 Tableau Cloud 中的“下載交叉表”選項相同的數據,選擇基於字母順序顯示在視圖上的第一個選項。

  1. 確保您的工作簿的參數是從維度字段生成的
  2. 創建並發布新視圖:將要過濾的維度放在“行”上,僅此而已。 它看起來像一個空的交叉表。
  3. 將行值提取到pandas DataFrame object 中。
import tempfile
import pandas as pd

new_view_id = "view_id_string_here"

tableau_auth = TSC.PersonalAccessTokenAuth(your_auth)
server = TSC.Server(your_server)

with server.auth.sign_in(tableau_auth):
    view = server.views.get_by_id(new_view_id)
    server.views.populate_csv(view)
    with tempfile.TemporaryFile() as file:
        for chunk in view.csv:
            file.write(chunk)
        file.seek(0)
        df = pd.read_csv(file)

此 dataframe 應包含您創建和發布的視圖的默認視圖中的所有尺寸值。 您可以根據需要使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM