Welcome to the beginner DataDev Quest Challenge for February 2025! This challenge is designed to help you learn how to use Tableau REST API, Tableau Server Client, and Tableau Developer Sandbox to update connection details within a workbook/datasource.

Challenge Overview

Objective:

Use Python (REST API or TSC) to update any combination of username, password, server URL and port for a connection or connections within a workbook or datasource.

If you need a small database to work with, we recommend the free tier of tembo.io.

Why this challenge?

Have you ever needed to programmatically update the URL, password, etc. of database connection within a workbook/datasource? What about many of them at once? This challenge will prepare you for this month’s intermediate challenge, where you’ll find all connections to a given database/server and update them at the same time.

To complete this challenge, you only need to update a connection in one workbook and one datasource.

Learning Goals

Submission Guidelines

Additional Resources


Getting Started

If you have not done so already, follow Cristian Saavedra-Desmoineaux’s step-by-step guide to setting your Tableau Developer Sandbox

1. Create or choose a workbook/datasource you want to interact with.
Make sure it’s not anything mission critical, as we will be breaking it temporarily

2. Make a change to the connection details that will break the connection, then click ‘Test Connection’
We want this to be ‘broken’ so that the changes made via the REST API/TSC will fix it
– In this example, I changed the port to an incorrect value

3. Be sure that you have Enabled the Personal Access Token (PAT) and configure your Personal Access Token (PAT). If you need help, follow Cristian’s Medium Post

4. If using tableauserverclient (TSC), make sure it is installed in your Python environment
– If you’re using python ‘requests’, ensure it’s available

5. If needed, review my article on Tableau REST API/TSC authentication.
– NOTE: You will need to use PAT authentication for this challenge


Challenge

Your challenge is to create a script/module that will do the following:


Hints

The following REST API/TSC endpoints and methods will help you with this quest:


Solution

Note: Be sure to create a virtual environment and pip install tableauserverclient

import tableauserverclient as TSC

SERVER_URL = 'YOUR-SERVER-URL' # e.g. https://10az.online.tableau.com
SITE_NAME = 'YOURSITENAME' # As it appears in the URL 
DATASOURCE_NAME = 'YOUR-DATA-SOURCE-NAME'

PAT_NAME = 'YOUR-PAT-NAME'
PAT_SECRET = 'YOUR-PAT-SECRET'

DB_HOST = 'YOUR-DATABASE-HOST'
DB_PORT = 'YOUR-DATABASE-PORT'
DB_USERNAME = 'YOUR-DATABASE-USERNAME'
DB_PASSWORD = 'YOUR-DATABASE-PASSWORD'

def update_datasource_connection(embed=True):
    server = TSC.Server(SERVER_URL, use_server_version=True)
    tab_auth = TSC.PersonalAccessTokenAuth(PAT_NAME, PAT_SECRET,site_id=SITE_NAME)

    with server.auth.sign_in(tab_auth):
        datasources = server.datasources.filter(name=DATASOURCE_NAME)

        if not datasources:
            raise ValueError(f"No datasource found with the name: {DATASOURCE_NAME}")
        
        for ds in datasources:
            server.datasources.populate_connections(ds)

            for connection in ds.connections:
                if connection.server_address == DB_HOST:
                    connection.server_port = DB_PORT
                    connection.username = DB_USERNAME
                    connection.password = DB_PASSWORD
                    connection.embed_password = embed

                    server.datasources.update_connection(ds, connection)
                    
                    print(f"Updated connection for datasource: {ds.name}")
                    

update_datasource_connection()
Python

Who Am I?

Oh, hi there! I’m Kyle Massey, Tableau Visionary and DataDev Ambassador. I’m also one of the founding members of DDQ, and I’m so happy you’re here with us! 😃 I’m passionate about all things data viz, software engineering, automation, etc. Always happy to connect: