Databricks Labs Blueprint

0.12.0 · active · verified Sun Mar 29

Databricks Labs Blueprint is a Python library that provides common building blocks and utilities for Databricks Labs projects. It offers Python-native pathlib-like interfaces for Databricks Workspace paths, tools for trivial terminal user interfaces (TUI), and utilities for managing application and installation state. The current version is 0.12.0, with a regular release cadence, typically monthly or bi-monthly, reflecting active development and maintenance. [1, 3]

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `WorkspaceClient` and use `WorkspacePath` to create and manage directories within your Databricks workspace. It expands a relative path to the user's home directory, creates the specified folder structure, verifies its existence, and then cleans it up. Ensure your Databricks SDK authentication (environment variables or CLI profile) is configured before running. [1]

import os
from databricks.sdk import WorkspaceClient
from databricks.labs.blueprint.paths import WorkspacePath

# Ensure DATABRICKS_HOST and DATABRICKS_TOKEN environment variables are set,
# or a Databricks CLI profile is configured.
# For local testing, you might run:
# export DATABRICKS_HOST='https://<your-databricks-instance>.cloud.databricks.com'
# export DATABRICKS_TOKEN='dapi...'

if not os.environ.get('DATABRICKS_HOST') or not os.environ.get('DATABRICKS_TOKEN'):
    print("Please set DATABRICKS_HOST and DATABRICKS_TOKEN environment variables or configure Databricks CLI.")
    # In a real quickstart, you might exit or raise an error here.
    # For demonstration, we'll use placeholder values that will likely fail.
    ws = WorkspaceClient(host=os.environ.get('DATABRICKS_HOST', 'https://example.cloud.databricks.com'), 
                         token=os.environ.get('DATABRICKS_TOKEN', 'dapi-fake-token'))
else:
    ws = WorkspaceClient()

print(f"Initialized WorkspaceClient for host: {ws.host}")

try:
    user_name = ws.current_user.me().user_name
    print(f"Current user: {user_name}")

    # Example: Working with user home folders
    folder_name = 'blueprint-test-folder'
    wsp = WorkspacePath(ws, f"~/{{folder_name}}/sub/dir")

    # Expand the user path and create the directory
    with_user = wsp.expanduser()
    print(f"Expanded path: {with_user}")
    with_user.mkdir()
    print(f"Directory '{with_user}' created.")

    # Verify existence
    assert with_user.is_dir()
    print(f"Directory '{with_user}' exists.")

    # Clean up (recursive rmdir)
    with_user.parent.parent.rmdir(recursive=True)
    assert not with_user.parent.parent.exists()
    print(f"Directory '{with_user.parent.parent}' and its contents removed.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure your Databricks environment is correctly configured (host, token, permissions).")

view raw JSON →