Interactive Simulation with Tableau and Jupytab
By: Brian Tribondeau
Tags: Jupyter Notebook, Tableau, Dataviz, Pandas, Python

Interactive Simulation with Tableau and Jupytab

Quickly create and deploy simulation tools and dashboards, by connecting directly Tableau to your Python Jupyter notebooks, and spend your time on the data, not on building visualizations Brian Tribondeau Apr 15·15 min read

Multi-Dimensional overview of the Iris Dataset

Overview

This article shows how to let Tableau, a leading interactive data visualization tool, access dynamic data instead of static data. This opens up new applications, such as dashboards that can display real-time data, or the ability of users to display dynamic predictions obtained by machine learning models.

We start with a gentle introduction to dynamic data access in Tableau, both through Tableau’s native language and through Python (via Analytics Extensions). Since this is a hands-on article, we describe how to install the necessary components (Tableau, and Jupyter notebooks) and then illustrate how to configure them and quickly obtain a Tableau dashboard that displays dynamic data based on machine learning.

Tableau

Tableau is a Business Intelligence tool widely used by the industry (along with PowerBI and QlikView). It allows non-technical people to create and deploy web application with a drag and drop interface.

The only drawback is that by default, the data need to be readily available in a supported database, which cannot itself be updated through the interface. You can however create blazingly fast interactive dashboards that connect to numerous data sources, and quickly get an overview of your data at scale.

Dynamic data in Tableau

Tableau’s scripting language

Tableau offers an Excel-like language that allows users to create custom calculations and manipulate different types of data (numeric, string, boolean). However the limitations of this scripting language can be very frustrating as there is no data manipulation like pivot tables, and even some simple transformations (like splitting a field into multiple columns) often require numerous steps.

Tableau’s scripting language is mainly there to transform values at the cell level, or to operate on the full data set for filtering or computing an aggregation. That said, your options are rather limited and you will have to mainly prepare your data outside of Tableau and create a precomputed dataset will all the data that you want to display.

But what if you want to create a simulation or apply some machine learning techniques and visualize the resulting changes after updating some parameters? The way Tableau works with data source simply does not allow this kind of operation, or that would force you to code your algorithm without any library, which is thoroughly impractical.

Analytics Extension to the rescue

To achieve real-time analytics with Tableau, or at least on-the-fly computations using custom algorithms, a very interesting feature of Tableau is hidden in Help → Settings and Performance → Manage Analytics Extension Connection…. It allows you to achieve what we would expect from a web application.

Analytics Extension — Image by Author

Here is where Analytics Extension Connections fit in the Tableau data ecosystem:

Data Sources in Tableau — Image by Author

Python in the Tableau ecosystem

The two places in Tableau where we can expect Python code are in data source creation, and on-the-fly computations (based on the displayed data).

Data source creation

The data source is often an existing database. It is possible to write a custom data injector that writes to this database. This approach should be fast, but it obviously requires a full-blown database, and also a scheduler that regularly invokes the data injector program.

Another approach consists in connecting directly to a custom data source. Tableau thus allows us to create custom Web Data Connector (WDC) and use the WDC API. A fair number of connectors are available, some of which may fit your need. If this is not the case you will have to create a custom connector, but be warned: the learning curve is steep.

Another alternative is that illustrated in this article: by using Jupytab, you can directly export your Pandas Dataframes to Tableau from a Jupyter notebook. (This approach uses the WDC API under the hood, so that you don’t have to wrangle with it.) By using Jupytab, you also get scheduled data refreshes for free, as you can use the Tableau internal scheduler to poll data on demand. A minor limitation of this approach is the maximum size of data sources: data sets with a billion rows typically do not fit easily in a Pandas Dataframe!

On-the-fly computations

Besides Jupytab, not many other Tableau tools perform on-the-fly data source creation and computation. We must however mention TabPy, which is Tableau’s own tool for interactive analytics in Python. It creates a server that allows you to use Python code in Tableau, by executing code on demand.

Jupytab serves exactly the same purpose, but with a few key differences:

All your Python code comes from a single notebook: you create the data source and expose it for Tableau there.

You don’t write Python code in Tableau, you always call a Python function from your notebook.

The set of available libraries is linked to the notebook. This means that you can create multiple execution environments with different sets of libraries and use all of them in Tableau, possibly from multiple notebooks.

If your main development tool is a Jupyter notebook with Pandas and some data science libraries, and you want to quickly create interactive dashboards ready for production, Jupytab is a tool that is worth a try.