SDK usage example

Follow these steps to track a simple Python script with the Databand SDK.

  1. Get an authorization token from Databand. The token is used in data pipelines when establishing a connection to Databand. For more information about getting your token, see Managing personal access tokens.

  2. For this example, use the following script:

# © Copyright Databand.ai, an IBM Company 2023

import datetime
import logging
import random

from dbnd import dbnd_tracking
from dbnd._core.task_build.dbnd_decorator import task
from dbnd._core.tracking.metrics import log_metric

databand_url = 'DATABAND_URL'
databand_access_token = 'ACCESS_TOKEN'

logger = logging.getLogger(__name__)

SIMPLE_SCENARIO_JOB_NAME = "dbnd_simple_python_tracking_scenario"
SIMPLE_SCENARIO_FUNC_NAME = "dbnd_tracking_func"
SIMPLE_SCENARIO_METRIC_INT_5 = "metric_check_int_5"


@task
def dbnd_tracking_func(check_time=datetime.datetime.now(), fail_chance=0.0):
    logger.info("Running Databand Task Sanity Check!")

    log_metric("metric_check", "OK")
    log_metric(SIMPLE_SCENARIO_METRIC_INT_5, 5)

    if random.random() < fail_chance:
        raise Exception("Failed accordingly to fail_chance: %f" % fail_chance)

    log_metric("Happiness Level", "High")

    logger.info("Your system is good to go! Enjoy Databand!")
    return "Databand has been checked at %s" % check_time


def dbnd_simple_tracking_scenario(job_name=SIMPLE_SCENARIO_JOB_NAME):
    with dbnd_tracking(
            conf={
                "core": {
                    "databand_url": databand_url,
                    "databand_access_token": databand_access_token,
                }
            },
            job_name=job_name,
            run_name="Pyhton_Tracking_Example_run",
            project_name="Pyhton_Tracking_Example_project",
    ):
        dbnd_tracking_func()


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    dbnd_simple_tracking_scenario()

  1. In the beginning of the script, we import the Databand library, but you can also install the library using: pip install databand.
    Python version 3.10 or later is required by the Databand SDK.

  2. Replace the databand_url and databand_access_token variables with the values for your Databand cluster. The URL format is https://YOUR_DATABAND_PORTAL_URL.

databand_url = 'INSERT_DATABAND_URL'
databand_access_token = 'INSERT_ACCESS_TOKEN'
  1. Save the changes that you made in the script and run it at least 5 times to generate metrics data. You can run the script in the debug mode, to review the SDK in more detail. As you run the script, you can monitor pipeline execution in the Pipelines page of the UI.