TabPy: Using Deployed Functions from Deployed Functions

If you don’t know what deployed functions in TabPy are consider reading the following:

With this post I’ll demonstrate with extremely simplified code how to use already deployed functions in other deployed functions.

Assuming TabPy is running on localhost machine and port 9004 in Python session I create and deploy simple function which capitalizes first letter of each string in the input parameter:

def my_func_1(s):
  return [x.capitalize() for x in s]

from tabpy.tabpy_tools.client import Client
client = Client('http://localhost:9004')
client.deploy('MyFunction1', my_func_1, 'Capitalize first letter of each string')

When the above is executed there is a function deployed which is accessible with Tableau SCRIPT_...('return tabpy.query('MyFunction1', _arg1)['response']', ...) expression. But what we are looking for is calling that function from another function.

When a function is deployed it is available in TabPy context for user script meaning you can use function name (the name the function was declared with, not the name given to when deployed). Here is how the function can be called:

def my_func_2(s):
  c = my_func_1(s)
  return ['_' + x + '_' for x in c]

client.deploy('MyFunction2', my_func_2, 'Underscore and capitalize each string')

Things to pay attention in the code sample above:

  1. The first deployed function is called by its Python name my_func_1 and not with the name it deployed with.
  2. Deployed functions (both MyFunction1 and MyFuction2) are used in Tableau calculations with their deployed names:

One caution I need to make here: How deployed functions are presented and accessible in the context of a script running in TabPy is not documented and not guaranteed to work in the future. The recommended way to reuse Python code is to create packages or use standalone modules as explained in How to use Python modules for TabPy scripts in Tableau post.

Out-of-the-box models in TabPy

Did you know TabPy has some data science models ready to use which are installed in your Python environment as a part of TabPy package?

But first what are TabPy models? Those simply are Python functions “preserved” in TabPy and available for being used in Tableau scripts. Here’s an explanation for how to deploy a function into TabPy – Deploying a Function. And this page shows how to use deployed functions in Tableau calculations – Using Deployed Functions.

Mentioned above documentation and examples should be enough for you to start on creating, deploying and using TabPy models (or deployed functions if you prefer that term).

As I mentioned above TabPy ships with some models which only need to be deployed. And deployment for them is as easy as running tabpy_deploy_models command in your terminal window after installing TabPy package. All the models are deployed at once. Remember you need TabPy running for the models to be deployed.

The following models are available at the moment I am writing this text:

  • Principal Component Analysis (PCA).
  • Sentiment Analysis.
  • T-Test.
  • Analysis of Variants (ANOVA).

The explanation for each of the models and how to invoke them in Tableau calculations can be found at the Predeployed Functions page.