Introducing AltTabPy - a Simple Alternative to TabPy

Version 7

    What is it?

    If you've searched on how to harness the collective powers of Tableau and Python, chances are you've already come across TabPy. So what does AltTabPy do differently?

     

    1. It provides a dead simple installation process
    2. It emphasizes scalability with a modern code base
    3. It masters the core functionality of executing Python code

     

    AltTabPy has the advantage of a simplified code base which, given the timing of its inception, is not burdened with legacy Python compatibility. At only 5% of the size of TabPy's code base, AltTabPy presents a clean and minimalist approach to harmonizing Python and Tableau.

     

    AltTabPy does NOT provide some of TabPy's features like managing authentication and publishing code, but in doing so aims to make it dead simple to execute Python code in calculated fields, with the results of those expressions visualizable in Tableau.

     

    Installation and Startup

    Please note that the most up to date instructions on installation and usage can be found at the AltTabPy documentation. Instructions at time of writing are provided below for reference.

     

    Installing for the first time

    AltTabPy is available on PyPi so be sure to grab it from there if you have not already:

     

    pip install alt-tabpy
    

     

    Starting the Application

    Once installed, you need to start the application from the command line:

     

    python -m alt_tabpy
    

     

    By default this will be listening on port 9004 (alternate ports can be specified using the --port command line flag):

    launched.png

     

    Connecting a Tableau Workbook

    With AltTabPy running you’ll need to tell Tableau Desktop where it can communicate with the service. To do so go to Help > Settings and Performance > Manage External Service Connection

    help_menu.png

     

    On the subsequent screen specify the URL and port where the service is running:

     

    host_input.png

     

    Test the connection and if all is working well you should get a success message!

    success.png

     

    Sample Usage

    For all of these examples we will use the Sample - Superstore datasource that comes with Tableau Desktop.

     

    Swapping the Case of All Text

    Let’s assume we wanted to reformat the Customer Name dimension in the sample dataset to make anything that is upper case lower and vice-versa. In Python you can achieve this using the swapcase string method as such:

     

    print(x.swapcase() for x in ["John Smith", "Jane Doe"])
    

     

    The equivalent function in AltTabPy looks something like this:

    swapcase.png

     

    Note the semantics required by Tableau to achieve this. Because the result of this expression is a string we need to use SCRIPT_STR (as opposed to SCRIPT_BOOL, SCRIPT_INT or SCRIPT_REAL). The Customer Name dimension in this case gets passed to the function as _arg1. Subsequent dimensions or measures would repeat this pattern of _arg2, _arg3.. _argn. The code block being passed then iterates over the values, returning back a list of values for the Tableau engine to subsequently processed. The result of this appears as follows:

    swapcase_result.png

     

    Replacing Characters via Regex

    You aren’t limited to builtin commands and methods - you can also import standard and third party libraries for use in your analyses!

     

    To illustrate, let’s imagine a scenario where we wanted to identify names containing at least one letter which was repeated twice. To put this in other words, we’d like some way to flag that the names Allen, Benny and Connor differ from Al, Benjamin and Conor given the former have repeating characters whereas the latter do not.

     

    Again in python you could do something like this:

     

    import re
    bool(re.search(r'((\w)\2){1,}', 'Connor'))  # True
    bool(re.search(r'((\w)\2){1,}', 'Conor'))  # False
    

     

    Here’s the subsequent calculation in Tableau:

    repeats_char.png

     

    And our result confirming the calculation:

    repeats_char_result.png

     

    Conclusion

    AltTabPy will always stay true to its goal of making Python and Tableau integration dead simple, but that doesn't mean it won't continue to evolve! To make sure you are up to date with the latest and greatest features, be sure to check out the:

     

     

    In the spirit of open source, contributions from the community to improve and advance the tool will always be welcome! In case of any apparent bugs or enhancement requests feel free to open an issue on GitHub and ensure the continued evolution of the tool.