2 Replies Latest reply on Sep 7, 2018 6:55 AM by Roger Pouly

    Read data from tableau server with Python

    Roger Pouly

      Hello everyone !

       

      I have a Python script that automatically get some workbook views, download them as a jpg, then send it in e-mail to most of my company.

       

      I would want to be able to check one point of data in my workbook to be sure (like total sales of the day) that Tableau didn't glitch out that day and I'm not sending an e-mail with innacurate data.

       

      I tried to look at Tableau Server Client samples but did not find a way to retrieve data from a workbook.

       

      Do you know any way to do it ?

       

      import tableauserverclient as TSC
      import time
      import argparse
      import getpass
      import logging
      
      
      
      
      server = 'XXX'
      username = 'XXX'
      password = 'XXX'
      view_name = 'Global ACTIVITY DAILY'
      filepath = 'Images/Images_daily.png'
      import tableauserverclient as TSC
      
      
      
      password = "XXX"
      
      
      # Step 1: Sign in to server.
      tableau_auth = TSC.TableauAuth(username, password)
      server = TSC.Server(server, use_server_version=True)
      
      
      
      
      
      
      
      
      
      
      
      
      with server.auth.sign_in(tableau_auth):
      
      
          req_option = TSC.RequestOptions()
          req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.Equals, 'DAILY MAIL'))
      
      
          all_workbooks, pagination_item = server.workbooks.get(req_option)
      
      
          workbook_item = all_workbooks[0]
      
      
          server.workbooks.populate_views(workbook_item)
          all_project_items, pagination_item = server.projects.get()
          all_workbooks, pagination_item = server.workbooks.get()
          # Step 2: Query for the view that we want an image of
          req_option = TSC.RequestOptions()
          req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
          TSC.RequestOptions.Operator.Equals, view_name))
          all_views, pagination_item = server.views.get(req_option)
          if not all_views:
             raise LookupError("View with the specified name was not found.")
          view_item = all_views[0]
      
          # Step 3: Query the image endpoint and save the image to the specified location
          image_req_option = TSC.ImageRequestOptions(imageresolution=TSC.ImageRequestOptions.Resolution.High)
          server.views.populate_image(view_item, image_req_option)
      
      
          with open(filepath, "wb") as image_file:
              image_file.write(view_item.image)
      
      
          print("View image saved to {0}".format(filepath))