avatarElliott Stam

Summary

This web content provides a tutorial on using Python and the Tableau Server REST API to interact with Tableau Server, including instructions on installing necessary libraries, setting up a Jupyter notebook, configuring server credentials, and executing API calls.

Abstract

The provided web content serves as a comprehensive guide for users looking to automate interactions with Tableau Server using Python. It outlines a series of steps to connect to and manipulate Tableau Server via its REST API. The tutorial begins with the prerequisite of having Python 3 installed and then guides the reader through installing the tableau-api-lib library using pip. It advises users to use Jupyter Lab for executing Python code, which can be installed via pip if not already available. The content includes code snippets for configuring Tableau Server credentials and connecting to multiple Tableau Server environments. It demonstrates how to establish a connection with Tableau Server, sign in, retrieve server information, and sign out properly. The tutorial emphasizes the importance of the tableau-api-lib in mirroring the Tableau Server REST API reference, ensuring that any endpoint documented by Tableau has a corresponding method in the library. The article concludes with a block of code that encapsulates the setup process, providing a quick start for users to begin leveraging the REST API.

Opinions

  • The author suggests that using Jupyter Lab is beneficial due to it being free and awesome.
  • The tutorial is part of a series that aims to help users tap into Tableau Server's potential, likening the process to accessing a keg.
  • The author emphasizes the ease of accessing the JSON content from the HTTP responses, which contains the desired data.
  • The tableau-api-lib is praised for its alignment with Tableau's REST API reference, facilitating a straightforward mapping of documented endpoints to library methods.
  • The importance of signing out after completing interactions with the server is highlighted to ensure the security of the auth token.
  • The tutorial is designed to be beginner-friendly, providing step-by-step instructions and assuming no prior knowledge of Python or the Tableau Server REST API.

Tableau REST API: tableau-api-lib tutorials

Tableau Server on tap: getting started

How to use Python and the Tableau Server REST API to interact with Tableau Server

Photo by Louis Hansel on Unsplash

This is also covered in our YouTube channel (Devyx): check it out.

Let’s get right into it. This tutorial walks through establishing a connection to Tableau Server, and provides a starting point to control Tableau Server via REST API calls. This is part of a series on how to tap Tableau Server like a keg, and solve various needs using Tableau Server’s REST API.

These tutorials assume you have Python 3 installed already. If you do not have Python 3 yet, this will get you started: guide to install Python.

Need help automating your Tableau Server environment? Learn more.

Step 1: Install the open source library tableau-api-lib

Run the following on your command line:

pip install --upgrade tableau-api-lib

Step 2: Open a Jupyter notebook (or your Python IDE of choice)

In this tutorial, we will use Jupyter Lab. It’s free and it’s awesome. If you don’t have it, you can easily install it by running the following in your command line:

pip install jupyterlab

To open a Jupyter notebook, run this next line:

jupyter lab

This will land you in the Jupyter Lab environment, where you can open a notebook and run Python code.

Step 3: copy this format to enter your Tableau Server credentials

Copy the code below into a cell in your Jupyter notebook.

tableau_server_config = {
        'tableau_prod': {
                'server': 'https://YourTableauServer.com',  
                'api_version': '3.4',
                'username': 'username',
                'password': 'password',
                'site_name': 'Your Site Name',
                'site_url': 'YourSiteContentUrl'
        }
}

Edit the values to match the configuration for your Tableau Server environment. If you don’t know what API version your server is on, you can look it up here: Tableau Server REST API versions.

Run the code within a cell of your Jupyter notebook.

Note that the ‘site_url’ is not the full URL of the Tableau Server, but is instead the portion of the URL that indicates the specific site you are logging into. For example, if I am logging into a site named ‘My Tableau Site’, the ‘site_url’ (also known as the content URL) would be ‘MyTableauSite’, or something similar. You can verify what your ‘site_url’ is by visiting your site in your browser, and identifying how your site appears in the resulting URL path.

If you want to list more Tableau Server connections and connect to them as well, imitate the pattern provided for ‘tableau_prod’. For example, you could make ‘tableau_dev’ and define an environment for a different Tableau Server.

tableau_server_config = {
        'tableau_prod': {
                'server': 'https://YourTableauServer.com',  
                'api_version': '3.4',
                'username': 'username',
                'password': 'password',
                'site_name': 'Your Site Name',
                'site_url': 'YourSiteContentUrl'
        },
        'tableau_dev': {
                'server': 'https://YourDevServer.com',  
                'api_version': '3.4',
                'username': 'username',
                'password': 'password',
                'site_name': 'Your Dev Site Name',
                'site_url': 'YourDevSiteContentUrl'
        }}

Step 4: import the tableau-api-lib

Run this code in a new cell in your Jupyter notebook:

from tableau_api_lib import TableauServerConnection

Step 5: establish a connection with Tableau Server

Run the code below to sign in to Tableau Server.

conn = TableauServerConnection(tableau_server_config)

By default, TableauServerConnection uses the ‘tableau_prod’ environment. If you want to connect to a different environment, simply specify that as an argument:

conn = TableauServerConnection(tableau_server_config, env='tableau_dev')

Step 6: sign into the server and verify that you are connected

To sign into the server, run the code below:

conn.sign_in()

Now, to verify that you are signed in, let’s try grabbing information from Tableau Server.

your_server_info = conn.server_info()

The tableau-api-lib Python library returns the HTTP responses from Tableau Server, and the meat of what you actually want is passed as a JSON object. To read that data, we can access the json() portion of the response.

your_server_info.json()

You should see something similar to my results below:

The JSON response body from calling the server_info() method

Here’s what you can expect to see if you don’t access the JSON portion of the response:

The response itself contains multiple entities, among those is the JSON content we want.

Step 7: use the Tableau Server REST API reference to view available methods

The tableau-api-lib strives to mirror the Tableau Server REST API reference such that if you find an endpoint documented in Tableau’s reference, you will find a matching method in this library.

For example, we have used the sign_in() and server_info() methods. How would you know these exist? Because they are documented here.

Explore the methods you now have at your fingertips!

Step 8: sign out to close your connection

It’s never a bad idea to sign out after you’re done. The auth token your connection is using will remain valid for some time unless you explicitly sign out from your active session.

conn.sign_out()

And that does it, you’re now using tableau-api-lib to tap into your Tableau Server!

Here’s one block of code you can copy and paste to get started more quickly:

from tableau_api_lib import TableauServerConnection
tableau_server_config = {
        'tableau_prod': {
                'server': 'https://YourTableauServer.com',  
                'api_version': '3.4',
                'username': 'username',
                'password': 'password',
                'site_name': 'Your Site Name',
                'site_url': 'YourSiteContentUrl'
        }
}
conn = TableauServerConnection(tableau_server_config)
conn.sign_in()
print(conn.server_info().json())
conn.sign_out()

Future posts will reference this as a starting point, so that we can dive straight into actually doing things with the REST API and avoid repeating the details of getting up and running.

I hope you found this tutorial helpful!

Python
Tableau
API
Jupyter Notebook
Tableau Server
Recommended from ReadMedium