TabPy: Combining Python and Tableau | by Bima Putra Pratama | Towards Data Science

Can we integrate the power of Python calculation with a Tableau?
That question was encourage me to start exploring the possibility of using Python calculation in Tableau, and I ended up with a TabPy.
So, What is TabPy? How can we use TabPy to integrating Python and Tableau?
In this article, I will introduce TabPy and go through an example of how we can use it.
TabPy is an Analytics Extension from Tableau which enables us as a user to execute Python scripts and saved functions using Tableau. Using TabPy, Tableau can run Python script on the fly and display the results as a Visualization. Users can control data being sent to TabPy by interacting in their Tableau worksheet, dashboard, or stories using parameters.
You can read more about TabPy in the official Github Repository:
github.com
I assume you already have Python installed in your system. If you don’t, you can install it first by going to https://www.python.org/ to download the python installation. Then you can install it in your system.
Next, we can install TabPy as a python package by using pip:
pip install tabpy
Once the installation success, we can run the services using the following command:
tabpy
If all goes well, you should see this:
By default, this service will be running in your localhost on port 9004. You can also verify it by open it in your web browser.
Now, let’s go to our Tableau and set up the service. I am using Tableau Desktop version 2020.3.0. However, there will be no difference in the previous version as well.
First, go to Help, then choose Settings and Performance and select Manage Analytics Extension Connection.
Then, you can set up the Server and Port. You can leave Sign in with a username and password blank, as we don’t set up credentials in our TabPy service.
Once done, click the Test Connection. If successful, you will see this message:
Congratulations!! Now, our Tableau is already connected with TabPy and ready to use.
There are two ways that we can use to do Python calculation:
Write code directly as Tableau calculated fields. The code then will be immediately executed on the fly in the TabPy server.
Deploy a function into the TabPy server that can be reachable as a REST API endpoint.
In this article, I will only show how to do the first method, which we will write code directly as Tableau calculated fields.
As an example, we will perform clustering to the Airbnb dataset that publicly available through the Tableau site, and you can download it using this link. We will cluster each zipcode based on their housing characteristics using several popular clustering algorithms.
In the first step, let’s import our data set to Tableau. This dataset has 13 columns.
As our primary goal is to see how we use TabPy, We will not focus on making the best possible model. Thus, we will only use the following variables in this dataset to perform clustering:
The median number of beds in each zip code
The average price in each zip code
The median number of ratings in each zip code
We need to create two parameters that will be used to select our clustering method and number of clusters, which are:
Cluster Numbers
Clustering Algorithm
We will create a python script as a calculated field in Tableau.
You can then insert the following script in a calculated field.
This code is wrapped in SCRIPT_REAL() function from Tableau and will do the following:
Import required Python libraries.
Scaling features with Standard Scaler
Combine Scaled Features and handling null values
Conditional to check which algorithm to use and do the following
Return clustering results as a list.
Then we will convert the results into String data type to make it as categorical data.
One more thing to notice is we need to do the Table Calculation in Zipcode. So we need to change the Default Table Calculation to Zipcode to make this code works.
Now, it’s time to visualize the results. I use a Zipcode to create a Map to visualize the clustering results. We can use the parameter to change the number of clusters.
Let’s celebrate coming up to this point! If you follow the step, you have been successfully integrating Python and Tableau. This integration is a beginning step for a more advanced use case using Tableau and Python.
I’m looking forward to seeing what you build with this integration!
Bima is a Data Scientist with Tableau Desktop Specialist Certification, who always eager to expand his knowledge and skills. He was graduated as a Mining Engineer and began his Data Science journey through various online programs from HardvardX, IBM, Udacity, etc. Currently, he is making impacts together with DANA Indonesia in building a cashless society in Indonesia.
If you have any feedback or any topics to be discussed, please reach out to Bima via LinkedIn. I’m happy to connect with you!
https://tableaumagic.com/tableau-and-python-an-introduction/
https://github.com/tableau/TabPy
https://public.tableau.com/en-us/s/resources
https://www.tableau.com/about/blog/2017/1/building-advanced-analytics-applications-tabpy-64916
https://www.tableau.com/about/blog/2016/11/leverage-power-python-tableau-tabpy-62077
https://towardsdatascience.com/tableau-python-tabpy-and-geographical-clustering-219b0583ded3
Some rights reserved