w.workspace: Workspace

class databricks.sdk.service.workspace.WorkspaceExt

The Workspace API allows you to list, import, export, and delete notebooks and folders.

A notebook is a web-based interface to a document that contains runnable code, visualizations, and explanatory text.

delete(path: str [, recursive: Optional[bool]])

Delete a workspace object.

Deletes an object or a directory (and optionally recursively deletes all objects in the directory). * If path does not exist, this call returns an error RESOURCE_DOES_NOT_EXIST. * If path is a non-empty directory and recursive is set to false, this call returns an error DIRECTORY_NOT_EMPTY.

Object deletion cannot be undone and deleting a directory recursively is not atomic.

Parameters:
  • path – str The absolute path of the notebook or directory.

  • recursive – bool (optional) The flag that specifies whether to delete the object recursively. It is false by default. Please note this deleting directory is not atomic. If it fails in the middle, some of objects under this directory may be deleted and cannot be undone.

download(path: str[, format: ExportFormat]) BinaryIO

Usage:

import io
import time

from databricks.sdk import WorkspaceClient
from databricks.sdk.service.workspace import ImportFormat

w = WorkspaceClient()

py_file = f'/Users/{w.current_user.me().user_name}/file-{time.time_ns()}.py'

w.workspace.upload(py_file, io.BytesIO(b'print(1)'), format=ImportFormat.AUTO)
with w.workspace.download(py_file) as f:
    content = f.read()
    assert content == b'print(1)'

w.workspace.delete(py_file)

Downloads notebook or file from the workspace

Parameters:
  • path – location of the file or notebook on workspace.

  • format – By default, ExportFormat.SOURCE. If using ExportFormat.AUTO the path is imported or exported as either a workspace file or a notebook, depending on an analysis of the item’s extension and the header content provided in the request.

Returns:

file-like io.BinaryIO of the path contents.

export(path: str [, format: Optional[ExportFormat]]) ExportResponse

Usage:

import time

from databricks.sdk import WorkspaceClient
from databricks.sdk.service import workspace

w = WorkspaceClient()

notebook = f'/Users/{w.current_user.me().user_name}/sdk-{time.time_ns()}'

export_response = w.workspace.export(format=workspace.ExportFormat.SOURCE, path=notebook)

Export a workspace object.

Exports an object or the contents of an entire directory.

If path does not exist, this call returns an error RESOURCE_DOES_NOT_EXIST.

If the exported data would exceed size limit, this call returns MAX_NOTEBOOK_SIZE_EXCEEDED. Currently, this API does not support exporting a library.

Parameters:
  • path – str The absolute path of the object or directory. Exporting a directory is only supported for the DBC, SOURCE, and AUTO format.

  • format

    ExportFormat (optional) This specifies the format of the exported file. By default, this is SOURCE.

    The value is case sensitive.

    • SOURCE: The notebook is exported as source code. Directory exports will not include non-notebook

    entries. - HTML: The notebook is exported as an HTML file. - JUPYTER: The notebook is exported as a Jupyter/IPython Notebook file. - DBC: The notebook is exported in Databricks archive format. Directory exports will not include non-notebook entries. - R_MARKDOWN: The notebook is exported to R Markdown format. - AUTO: The object or directory is exported depending on the objects type. Directory exports will include notebooks and workspace files.

Returns:

ExportResponse

get_permission_levels(workspace_object_type: str, workspace_object_id: str) GetWorkspaceObjectPermissionLevelsResponse

Get workspace object permission levels.

Gets the permission levels that a user can have on an object.

Parameters:
  • workspace_object_type – str The workspace object type for which to get or manage permissions.

  • workspace_object_id – str The workspace object for which to get or manage permissions.

Returns:

GetWorkspaceObjectPermissionLevelsResponse

get_permissions(workspace_object_type: str, workspace_object_id: str) WorkspaceObjectPermissions

Get workspace object permissions.

Gets the permissions of a workspace object. Workspace objects can inherit permissions from their parent objects or root object.

Parameters:
  • workspace_object_type – str The workspace object type for which to get or manage permissions.

  • workspace_object_id – str The workspace object for which to get or manage permissions.

Returns:

WorkspaceObjectPermissions

get_status(path: str) ObjectInfo

Usage:

import time

from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

notebook_path = f'/Users/{w.current_user.me().user_name}/sdk-{time.time_ns()}'

obj = w.workspace.get_status(path=notebook_path)

Get status.

Gets the status of an object or a directory. If path does not exist, this call returns an error RESOURCE_DOES_NOT_EXIST.

Parameters:

path – str The absolute path of the notebook or directory.

Returns:

ObjectInfo

import_(path: str [, content: Optional[str], format: Optional[ImportFormat], language: Optional[Language], overwrite: Optional[bool]])

Usage:

import base64
import time

from databricks.sdk import WorkspaceClient
from databricks.sdk.service import workspace

w = WorkspaceClient()

notebook_path = f'/Users/{w.current_user.me().user_name}/sdk-{time.time_ns()}'

w.workspace.import_(content=base64.b64encode(("CREATE LIVE TABLE dlt_sample AS SELECT 1").encode()).decode(),
                    format=workspace.ImportFormat.SOURCE,
                    language=workspace.Language.SQL,
                    overwrite=True,
                    path=notebook_path)

Import a workspace object.

Imports a workspace object (for example, a notebook or file) or the contents of an entire directory. If path already exists and overwrite is set to false, this call returns an error RESOURCE_ALREADY_EXISTS. To import a directory, you can use either the DBC format or the SOURCE format with the language field unset. To import a single file as SOURCE, you must set the language field.

Parameters:
  • path – str The absolute path of the object or directory. Importing a directory is only supported for the DBC and SOURCE formats.

  • content

    str (optional) The base64-encoded content. This has a limit of 10 MB.

    If the limit (10MB) is exceeded, exception with error code MAX_NOTEBOOK_SIZE_EXCEEDED is thrown. This parameter might be absent, and instead a posted file is used.

  • format

    ImportFormat (optional) This specifies the format of the file to be imported.

    The value is case sensitive.

    • AUTO: The item is imported depending on an analysis of the item’s extension and the header

    content provided in the request. If the item is imported as a notebook, then the item’s extension is automatically removed. - SOURCE: The notebook or directory is imported as source code. - HTML: The notebook is imported as an HTML file. - JUPYTER: The notebook is imported as a Jupyter/IPython Notebook file. - DBC: The notebook is imported in Databricks archive format. Required for directories. - R_MARKDOWN: The notebook is imported from R Markdown format.

  • languageLanguage (optional) The language of the object. This value is set only if the object type is NOTEBOOK.

  • overwrite – bool (optional) The flag that specifies whether to overwrite existing object. It is false by default. For DBC format, overwrite is not supported since it may contain a directory.

list(path: str[, notebooks_modified_after: int, recursive: bool = False]) ObjectInfo

Usage:

from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

names = []
for i in w.workspace.list(f'/Users/{w.current_user.me().user_name}', recursive=True):
    names.append(i.path)
assert len(names) > 0

List workspace objects

Parameters:

recursive – bool Optionally invoke recursive traversal

Returns:

Iterator of workspaceObjectInfo

mkdirs(path: str)

Create a directory.

Creates the specified directory (and necessary parent directories if they do not exist). If there is an object (not a directory) at any prefix of the input path, this call returns an error RESOURCE_ALREADY_EXISTS.

Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

Parameters:

path – str The absolute path of the directory. If the parent directories do not exist, it will also create them. If the directory already exists, this command will do nothing and succeed.

set_permissions(workspace_object_type: str, workspace_object_id: str [, access_control_list: Optional[List[WorkspaceObjectAccessControlRequest]]]) WorkspaceObjectPermissions

Set workspace object permissions.

Sets permissions on a workspace object. Workspace objects can inherit permissions from their parent objects or root object.

Parameters:
  • workspace_object_type – str The workspace object type for which to get or manage permissions.

  • workspace_object_id – str The workspace object for which to get or manage permissions.

  • access_control_list – List[WorkspaceObjectAccessControlRequest] (optional)

Returns:

WorkspaceObjectPermissions

update_permissions(workspace_object_type: str, workspace_object_id: str [, access_control_list: Optional[List[WorkspaceObjectAccessControlRequest]]]) WorkspaceObjectPermissions

Update workspace object permissions.

Updates the permissions on a workspace object. Workspace objects can inherit permissions from their parent objects or root object.

Parameters:
  • workspace_object_type – str The workspace object type for which to get or manage permissions.

  • workspace_object_id – str The workspace object for which to get or manage permissions.

  • access_control_list – List[WorkspaceObjectAccessControlRequest] (optional)

Returns:

WorkspaceObjectPermissions

upload(path: str, content: bytes[, format: ImportFormat, language: Language, overwrite: bool = False])

Usage:

import io
import time

from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

notebook = f'/Users/{w.current_user.me().user_name}/notebook-{time.time_ns()}.py'

w.workspace.upload(notebook, io.BytesIO(b'print(1)'))
with w.workspace.download(notebook) as f:
    content = f.read()
    assert content == b'# Databricks notebook source\nprint(1)'

w.workspace.delete(notebook)

Uploads a workspace object (for example, a notebook or file) or the contents of an entire directory (DBC format).

Errors:
  • RESOURCE_ALREADY_EXISTS: if path already exists no overwrite=True.

  • INVALID_PARAMETER_VALUE: if format and content values are not compatible.

Parameters:
  • path – target location of the file on workspace.

  • content – the contents as either raw binary data bytes or a file-like the file-like io.BinaryIO of the path contents.

  • format – By default, ImportFormat.SOURCE. If using ImportFormat.AUTO the path is imported or exported as either a workspace file or a notebook, depending on an analysis of the item’s extension and the header content provided in the request. In addition, if the path is imported as a notebook, then the item’s extension is automatically removed.

  • language – Only required if using ExportFormat.SOURCE.