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.

TabPy: how do I know what models are available?

Pre…

TabPy allows deploying functions that can be called from Tableau side (more technical details are at Using Deployed Functions documentation page). With that feature, you may create a repository of models (this is what I call deployed functions in this post) with many entries with time. And at some moment the question of getting the list of all available models arises.

There are at least 3 ways to find out what models are available for a specific instance of TabPy. Let’s look at them.

Option 0. Index Page (updated Feb 02, 2020)

With TabPy v0.9.0 release you can just open your TabPy instance host:port in a browser and deployed models together with some other data will be displayed on the page.

Option 1. TabPy Logs

TabPy outputs some logging data in the console and the same log entries are preserved in a log file. The lines you are looking for have “Load endpoint: ” with a model name following it (endpoint here is just another name for a deployed function). In the screenshot below you can see models PCA, Sentiment Analysis, ttest, add, and anova being loaded on TabPy startup and available for being used in Tableau calculations.

Option 2. TabPy State File

On TabPy startup, it informs in the log output about state file location (example in the screenshot below). State file is used by Tornado web-server which TabPy is built around.

The log entry shows the location for the state file. In the file itself, there is [Query Objects Service Versions] section which lists all the deployed models. As you can see the same models you can find in TabPy logs are listed in the section.

Option 3. Use the API, Luke

In the Invoking TabPy API with Postman post, I explained how to use Postman to call TabPy API. And using the API is the best way for this scenario as well – implementation may (and will) change in the future (what is logged, how and where, how functions are deployed and preserved, where the state is preserved and so on) but the API hides all those details.

As documented for /endpoints method it returns a list of all deployed models. And Postman file in TabPy repo has the method in the collection. Simply use that method (specify your TabPy address) and you’ll see something like this:

Returned JSON lists all the deployed models and their properties.

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.