Databricks Labs Blueprint
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
- breaking In `v0.11.3`, the unmarshalling of JSON floating-point values was fixed. Previously, JSON floats might have been silently truncated to integers. The updated functionality now raises a `SerdeError` when precision would be lost during conversion from float to integer. [release notes]
- deprecated Starting with `v0.11.0`, using `Any` and `object` as type annotations on data classes for marshalling is deprecated and will issue a `DeprecationWarning`. [release notes]
- gotcha In versions prior to `v0.9.3`, there was an issue where `databricks-sdk` config objects could be unintentionally overridden when creating installation config files. This could lead to unexpected behavior or incorrect workspace configurations. [release notes]
Install
-
pip install databricks-labs-blueprint
Imports
- WorkspacePath
from databricks.labs.blueprint.paths import WorkspacePath
- Prompts
from databricks.labs.blueprint.tui import Prompts
- ProductInfo
from databricks.labs.blueprint.wheels import ProductInfo
Quickstart
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).")