Python Integration with Tableau Using TabPy

TabPy is a new API that is used to expand the Tableau’s features by executing Python Scripts and functions using Tableau’s Table Calculation.

TabPy gives the power to leverage many Machine-Learning libraries.

TabPy permits Tableau to execute python code remotely.

It comprises of two Parts:

  1. Tornado site package which is responsible for executing the python code remotely through REST API’s.
  2. Tools Library used for endpoint deployment based on the python functions used.

Download and Install Tableau Desktop

  1. Go to https://www.tableau.com/products/desktop
  2. Click the “Try Now” button
  3. Download the trial Pack available for 14 days trial period, by providing your email address.
  4. The latest tableau version will get downloaded.
  5. Run the extension file by double clicking it and accept the terms and condition.
  6. Click Install
  7. Once Installed, open the Tableau desktop, and fill in the details on tableau registration form.
  8. Click the “Start Trail Now” button. The Tableau Desktop screen will get opened. Now, we are done with Tableau desktop Installation.

Download and Install Python

  1. Go tohttps://www.python.org/downloads/.
  2. Download the latest version and double click the Python extension and install the python.
  3. Add the python path and python script path to the system environmental variable as shown below:
C:\Users\YourUserName\AppData\Local\Programs\Python\Python<version>
C:\Users\YourUserName\AppData\Local\Programs\Python\Python<version>\Scripts

4. Open the command prompt and type the command.

python --version. If you see the version downloaded, then python is working fine.
If you face error like below

Traceback (most recent call last):
File "tabpy.py", line 279, in
class EndpointsHandler(ManagementHandler):
File "tabpy.py", line 287, in EndpointsHandler
@tornado.web.asynchronous
AttributeError: module 'tornado.web' has no attribute 'asynchronous'

Downgrade the tornado version to pip install Tornado==5.1.1

Connecting Tableau and Tabpy Server

  1. Open the Tableau Desktop
  2. Under Help dropdown, choose Settings and Performance and then click Manage Analytics Extension Connection.
  3. In the Analytics Extension Connection dialog box,enter the following data.
    • Under Select an External Service, choose TabPy/External API
    • Enter localhost in Server field.
    • In Port, type 9004
  4. Clicking the Test Connection will show a message like “Successfully Connected to external service”.

Building Worksheets in Tableau Desktop

Note: Used Sample superstore dataset for this demo purpose.

Scenario: Find out the states with Profit is less than zero (Profit < 0)

  1. Connect to the dataset by clicking the Text file under To a file and choose the sample superstore dataset which is in (.csv) format. If it is (.xls or .xlsx), connect to Microsoft excel under To a File.
  2. Next, create the Calculated field as shown below using python script:

3. Now, Drag State and calculated field created named here as (Profitable) to the Rows Shelf.

4. Drag Profit under measures to the Columns shelf

5. Drag Calculated Field (Profitable) to the Color Marks.

Now, we can get the state name with profit < 0 and they are differentiated based on color as well as shown in the image below:

Note: The output from the calculated field is either True or False since we used Boolean script (SCRIPT_BOOL). Please refer to the image attached above.

Limitations of TabPy

  1. While extracting data, Python generated data will not be extracted.
  2. When working with large dataset, we must wait for more time for every time the script runs.
  3. To run the script, we should add it to the view.

References

https://www.tableau.com/about/blog/2016/11/leverage-power-python-tableau-tabpy-62077#:~:text=When%20you%20use%20TabPy%20with,in%20Tableau%20enables%20powerful%20scenarios.

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.