1 2 3 Previous Next 42 Replies Latest reply on Feb 19, 2020 7:06 PM by Nathan Mannheimer

    Tableau Integration with Python - Step by Step

    Pawan Kumar

      [UPDATE PLEASE READ --- TabPy has recently been updated and the install instructions below are now only for legacy purposes. Please find more information on TabPy here (GitHub - tableau/TabPy: Execute Python code on the fly and display results in Tableau visualizations: ) and to install, follow the instructions here (TabPy/server-install.md at master · tableau/TabPy · GitHub )]. Feel free to contact me at the email below with any comments or questions!

      --Nathan Mannheimer (nmannheimer@tableau.com), Product Manager Analytics Integrations


      Tableau Integration with Python – Knowledge Article Compilation By Pawan Kumar

      This course is intended to show how to leverage Python to extend Tableau capabilities and visualize outputs from Python.

      This is not a Tutorial on Python.

      What is Python

      • Python is a widely used general-purpose programming language, popular among academia and industry alike.
      • It provides a wide variety of statistical and machine learning techniques, and is highly extensible.
      • Together, Python and Tableau is the data science dream team to cover any organization’s data analysis needs.
      • In 2013 Tableau introduced the R Integration [since Tableau 8.1], the ability to call R scripts in calculated fields using R Server [ Rserve()].
      • With the release of Tableau 10.1, you can use Python scripts as part of your calculated fields in Tableau, just as you’ve been able to do with R
      •    The Python Integration happens through the   Tableau Python Server -  [ TabPy ] .

      Install tableau python server : TabPy

      Extract TabPy-master.zip

      • Within the TabPy-master directory, execute setup.bat (or setup. sh  if you are on Mac).
      • This script downloads and installs Python, TabPy and all necessary dependencies.
      • After completion, TabPy starts up and listens on port 9004.

      Key Points During InstallationTabPy installation takes a while and many time you're not successful on your first try.Key Points that needs to be taken care :

      • You have Python 3.0 and not the required Python 2.7.
      • You might have both versions, but your primary is the 3.0 version.
      • When you run the python tableau server set up file for the first time , it shows the message installing Anaconda ( even if Anaconda is there ).
      • Sometimes It may take more than hour to get the final confirmation message

      You may also get the below even if you have python installed. Have patience, let it keep running. (can take hours. If it get closed run it again, till you get success message).  You can get error, but run the setup file again, till finally it gets installed ( Couple of Try running the setup file ,does the trick )

      • When you get the below message it means the python server is successfully installed and running fine .
      • Also Note the path ( highlighted in red below ) for starting python server next time or else you will keep doing the same process again and again

      • So only the first time the python server takes time to run . From next time you need to go to your location as above and do startup and this gets started instantaneously.
      • So for next time to start python tableau server (tabpy) ,go to the below path

      cd c:\Users\User Name\Anaconda\envs\Tableau-Python-Server\Lib\site-packages\tabpy_serverAnd type → startup  and press enterSetting Up Tableau Desktop with Python
      Configure  a  TabPy Connection on TableauOn the Help menu in Tableau Desktop choose Settings and Performance > Manage External Service Connection to open the TabPy connection dialog box.

      Enter or select a server name using a domain or an IP address.
      The drop-down list includes localhost and the server you most recently connected to.
      Specify a port. Port 9004 is the default port for TabPy servers.
      Click Test Connection.
      Click OK.
      Pass Expressions to Python

      • In order to let tableau know that the calculations need to go to Python, it must be passed through one of the 4 functions.
      • These 4 functions are : SCRIPT_BOOL , SCRIPT_INT , SCRIPT_REAL , SCRIPT_STR
      • Python Functions are computed as Table calculations in Tableau.
      • Since these are table calculations, all the Fields being passed to Python must be aggregated like Sum(PROFIT), MIN(Profit), Max (Profit), ATTR( Category) etc.

      Python Functions in Tableau
      Run a Python script on TableauSCRIPT_BOOLReturns a Boolean result from the specified expression. The expression is passed directly to a running external service instance. In Python expressions, use _argn (with a leading underscore ) to reference parameters ( _arg1, _arg2, etc.).

      • In this python example, _arg1 is equal to SUM([Profit])
      • All the Fields being passed to python must be aggregated like Sum(PROFIT), MIN(Profit), Max (Profit), ATTR( Category) etc..


      SCRIPT_BOOL : Finding Profit Greater Zero by python


      PythonBoolPositive : Python Calculated Field Function Code


      lst= []

      for i in _arg1 :


      return lst





      SCRIPT_INT – Example → Multiply Sales with 2 from Python


      PythonIntegerMultiplyBy2 : Python Calculated Field Function Code


      lst= []

      for i in _arg1 :


      return lst




      SCRIPT_REAL – Example  - Finding log of sales by Python

      PythonRealLog : Python Calculated Field Function Code


      import math

      lst = []

      for i in _arg1:

          if math.isnan(i):


          else :


      return lst",



      SCRIPT_STR – Concatenate Two Strings using Python

      PythonStringConcatenate : Python Calculated Field Function Code


      lst= []

      for i in range(0,len(_arg1)) :


      return lst




      Finding correlation coefficient of Sales & Profit by Python

      PythonCorrCoeff: Python Calculated Field Function Code


      import numpy as np

      return np.corrcoef(_arg1,_arg2)[0,1]",



        1 2 3 Previous Next