3 Replies Latest reply on Mar 14, 2019 5:50 AM by Nagarajan .K Raj

    How to Integrate python with tableau

    Vandana Sharma

      Hi I tried integrating tableau with python although python and tabpy server were installed in different directories(possible problem).

      Further I tried changing python directory but was unable to configure it with VS code. Any thoughts?

        • 1. Re: How to Integrate python with tableau
          Adrian Chandley

          In python I think you either need to:

          1. create a python package from your code and install it

               Likely overkill

          2. put your code in the same directory as the calling module

               Likely this will be TabPy/tabpy-server/tabpy_server

          3. tell python where to search for you code - changing directory isn't sufficient, the code needs to be in the system path

               Fortunately you can add to the path from within python:

                    import sys

                    sys.path.insert(0, '<YOUR DIRECTORY>')

                    from <YOURMODULE> import <YOUR REFERENCE>

           

          There are a few quirks, for example it complains about the "*" if you try "from xzy import *"

           

          To keep the code in the tableau calculated field simple I use something like this:

           

               SCRIPT_INT("

               import sys                                             # (delete this line if module is in same directory)

               sys.path.insert(0, '/home/tabpy')           # (delete this line if module is in same directory)

               from mycode import process

               return process(locals())

               ",

               sum([Col1]), sum([Col2]), sum([Col3])      # these are the columns or other parameters I want to work on

               )

           

          I am using 'locals()' to pass all the arguments to my python module as tableau provides the args as separate variables rather than an array and so passing everything down to the next routine keeps the embedded code cleaner (IMHO).

           

          Then in my python module, (in this case /home/tabpy/mycode.py)

           

          def process(argsIn):

            args = []                                   # collect args

            argc = 1

            while True:

              arg = "_arg" + str(argc)          # name of next arg, eg _arg<n>

              if arg not in argsIn: break       # not there?, stop

              args.append(argsIn[arg])       # add this one to the list

              argc += 1

           

            # now i can do something with the args, and return a list of results

           

            argc = len(args)                         # number of columns

            argr = len(args[0])                      # number of rows

            res = []

            for r in range(argr):                     # for each row

              thisres = 0

              for c in range(argc):                  # sum all the arguments in the row

                thisres += args[c][r]

              res.append(thisres)                   # add sum of rows into results

            return res

           

          if __name__ == "__main__":

            pass

           

           

          Enjoy !

          2 of 2 people found this helpful
          • 2. Re: How to Integrate python with tableau
            Vandana Sharma

            Thank You I am definitely going to try this!

            • 3. Re: How to Integrate python with tableau
              Nagarajan .K Raj

              HI VANDANA,

               

              Try These method,

                  Tableau with Python Integration

               

              Steps :

              1. Install Tableau Desktop and Python and TabPy

                 

              2. Open Command prompt

                           3. Type the pip install tabpy-server”

                   

                     4. Type the  “pip install tabpy-client

                   

              5. check the tabpy server ID

              Users > Users > Appdata >Roaming >Microsoft >Windows>Startmenu>Programs>Python3.7>Open file location>Lib>Site_Package>tabpy _server>Start Up

              6. Goto > tableau >Help>Setting and Performance>Manage External Service connection>Open the Box

              7. I will try to integration

                State wise Profit report using Script_Boll function

              The Function Calculation

                       SCRIPT_BOOL("

              lst= []

              for i in _arg1 :

              lst.append(i>0)

              return lst",

              SUM([Profit])

               

              Thanks

              Nagarajan K

               

              Personally I have one question

              .