Python and Tableau
There are many areas where people use Python in their analytics workflow. The table below captures the main ones:
Basic data analysis (e.g. operations you can perform in SQL), as well as data visualization are not good Python use cases for Tableau — because Tableau does those things really well.
If you are a Python developer interested in applying your skills to Tableau, here are a few avenues to explore:
TabPy — a lightweight server that allows you to execute Python code in Tableau in 3 different scenarios:
dynamically invoked within a Tableau calculated field in a dashboard (Desktop, Cloud, or Server). Script_Real, Script_Int, Script_Str, Script_Bool are the calculated field functions that call your TabPy server. These functions are table calculations
invoked in the “script” step in Tableau Prep (Desktop, Server, no Cloud support as of Sept 2024) to either bring in data or transform existing data in your Prep flow
used as a data source for a Tableau workbook via Table Extensions (a vague name that I hope changes). Table Extensions executes Python code to retrieve data, load it into a data frame, then convert that data frame into a .hyper extract
Tableau’s REST API — you can use the Python wrapper or use the Requests library in Python to use this
The REST API allows you to perform admin tasks on your Cloud or Server site
Tableau’s Hyper API (formerly known as the Extract SDK) — there are also simpler, more user-friendly wrappers like PanTab
The Hyper API lets you create and query Hyper files within a script.
Third party softwares that create Tableau Hyper extracts use this behind the scenes
I’ll organize these tasks by functional area (mirroring the table above) and give a little more detail.
Advanced analytics
TabPy in calculations in Tableau Desktop/Web Authoring
You can execute Python code directly in a Tableau calculated field using TabPy (an Analytics Extension API).
These calculated fields use the 4 following table calculations to execute Python code:
Script_Real() → pass in aggregated fields from Tableau, return a Python list of real numbers
Script_Int() → pass in aggregated fields from Tableau, return a Python list of integers
Script_Bool() → pass in aggregated fields form Tableau, return a Python list of booleans
Script_Str() → pass in aggregated fields from Tableau, return a Python list of strings
Because they are table calculations, they will be computed on top of the existing aggregated result set.
ETL
Table Extensions
TabPy can also be used in the context of loading data into Tableau or performing transformations on your data.
Table Extensions allow you run a Python script to dynamically build an extract that you can use in your Tableau data model.
TabPy in Prep
Tableau Prep also has the ability to add a Python script into your flow:
Both Tableau Prep Scripts and Table Extensions leverage the TabPy Server.
Hyper API
Tableau’s Hyper API allows you to create an extract from any data you can access within a Python Script. This is a nice option if there is no native connector for a specific data source, and you are okay with the connection not being live. The Hyper API is often paired with a tool like Airflow to run recurring extracts.
Automation and Admin
The Tableau REST API can be invoked in Python scripts, allowing you to run administrative actions on your Tableau Cloud or Server environment. These actions include provisioning users, changing permissions on content, downloading content, publishing content, and more.