Skip to content

Python Dependency Example

This code-based example shows how to add a Python dependency to the container in order to interact with an external tool, such as Tableau.

Note

Container images are built with the DataKitchen Interface Layer and require specific files and file structures to be present in a container node. For more information, see GPC File Structure and Configuration.

File contents

config.json

The config.json file defines a Python dependency as well as a vault value for accessing Tableau. The tableauserverclient tool defined in config.json is for example purposes only. This version of the package is actually pre-installed in the GPC.

{
    "apt-dependencies": [ ],
    "dependencies": [
        "tableauserverclient==0.11"
    ],
    "keys": {
        "tableau_script": {
            "script": "tableau_test.py",
            "environment": {},
            "parameters": {
                "PASSWORD": "#{vault://tableau/password}"
            },
            "export": [
                "success"
            ]
        }
    }
}

tableau_test.py

The tableau_test.py script checks that the password parameter you want to inject exists in the globals array and is defined. This script also has some Jinja variables embedded directly.

import os
import traceback

import tableauserverclient as TSC

global success


# Validate parameters
if 'PASSWORD' not in globals() or not PASSWORD:
    LOGGER.error("Undefined PASSWORD")
    sys.exit(1)

try:
    # create an auth object
    tableau_auth = TSC.TableauAuth('{{tableauConfig.username}}', PASSWORD, '{{tableauConfig.content_url}}')

    # create an instance for your server
    server = TSC.Server('{{tableauConfig.url}}')

    # sign in to the tableau server
    server.auth.sign_in(tableau_auth)

    LOGGER.info(f'Tableau Server Version: {server.version}')
    success = True
except Exception as e:
    LOGGER.error(f'Failed to connect to Tableau:\n{traceback.format_exc()}')
    success = False

notebook.json

The notebook.json file holds the container configuration and test settings.

{
    "image-repo": "{{dockerhubConfig.image_repo.general_purpose}}",
    "image-tag": "{{dockerhubConfig.image_tag.general_purpose}}",
    "dockerhub-namespace": "{{dockerhubConfig.namespace.general_purpose}}",
    "dockerhub-username": "{{dockerhubConfig.username}}",
    "dockerhub-password": "{{dockerhubConfig.password}}",
    "tests": {
        "log_dockerhub_tool_instance": {
            "test-variable": "dockerhubConfig",
            "action": "log",
            "type": "test-contents-as-string",
            "test-logic": "dockerhubConfig",
            "keep-history": true,
            "description": "Logs the DockerHub tool instance."
        },
        "test_success": {
            "test-variable": "success",
            "action": "stop-on-error",
            "type": "test-contents-as-boolean",
            "test-logic": "success",
            "keep-history": true,
            "description": "Stops the OrderRun if success is False."
        }
    }
}