From 7e8a0aeb61e7c22bef7eabd137601bcc4d66d95a Mon Sep 17 00:00:00 2001 From: Lulzim Bilali Date: Wed, 12 Mar 2025 16:36:15 +0100 Subject: [PATCH 1/2] add format parameter to notebook definitions --- src/sempy_labs/_helper_functions.py | 2 +- src/sempy_labs/_notebooks.py | 49 +++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/sempy_labs/_helper_functions.py b/src/sempy_labs/_helper_functions.py index c6092490..35ed14c0 100644 --- a/src/sempy_labs/_helper_functions.py +++ b/src/sempy_labs/_helper_functions.py @@ -264,7 +264,7 @@ def create_item( lro_return_status_code=True, ) print( - f"{icons.green_dot} The '{name}' {item_type} has been successfully created within the in the '{workspace_name}' workspace." + f"{icons.green_dot} The '{name}' {item_type} has been successfully created within the '{workspace_name}' workspace." ) diff --git a/src/sempy_labs/_notebooks.py b/src/sempy_labs/_notebooks.py index dd870d46..2fd3c935 100644 --- a/src/sempy_labs/_notebooks.py +++ b/src/sempy_labs/_notebooks.py @@ -20,13 +20,21 @@ def _get_notebook_definition_base( - notebook_name: str, workspace: Optional[str | UUID] = None + notebook_name: str, + workspace: Optional[str | UUID] = None, + format: Optional[str] = None ) -> pd.DataFrame: (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) item_id = resolve_item_id(item=notebook_name, type="Notebook", workspace=workspace) + + if format == 'ipynb': + format = f"?format={format}" + else: + format = '' + result = _base_api( - request=f"v1/workspaces/{workspace_id}/notebooks/{item_id}/getDefinition", + request=f"v1/workspaces/{workspace_id}/notebooks/{item_id}/getDefinition{format}", method="post", lro_return_json=True, status_codes=None, @@ -53,7 +61,10 @@ def _get_notebook_type( def get_notebook_definition( - notebook_name: str, workspace: Optional[str | UUID] = None, decode: bool = True + notebook_name: str, + workspace: Optional[str | UUID] = None, + decode: bool = True, + format: Optional[str] = None ) -> str: """ Obtains the notebook definition. @@ -71,6 +82,9 @@ def get_notebook_definition( decode : bool, default=True If True, decodes the notebook definition file into .ipynb format. If False, obtains the notebook definition file in base64 format. + format : str, default=None + The only supported value is ipynb + If provided the format will be in standard .ipynb otherwise the format will be in source code format which is GIT friendly ipynb Returns ------- @@ -79,7 +93,7 @@ def get_notebook_definition( """ df_items = _get_notebook_definition_base( - notebook_name=notebook_name, workspace=workspace + notebook_name=notebook_name, workspace=workspace, format=format ) df_items_filt = df_items[df_items["path"].str.startswith(_notebook_prefix)] payload = df_items_filt["payload"].iloc[0] @@ -163,6 +177,7 @@ def create_notebook( type: str = "py", description: Optional[str] = None, workspace: Optional[str | UUID] = None, + format: Optional[str] = None ): """ Creates a new notebook with a definition within a workspace. @@ -182,20 +197,25 @@ def create_notebook( The name or ID of the workspace. Defaults to None which resolves to the workspace of the attached lakehouse or if no lakehouse attached, resolves to the workspace of the notebook. + format : str, default=None + If 'ipynb' is provided than notebook_content should be standard ipynb format + otherwise notebook_content should be GIT friendly format """ - notebook_payload = base64.b64encode(notebook_content).decode("utf-8") + notebook_payload = base64.b64encode(notebook_content.encode("utf-8")).decode("utf-8") definition_payload = { - "format": "ipynb", "parts": [ { - "path": f"{_notebook_prefix}.{type}", + "path": f"{_notebook_prefix}{type}", "payload": notebook_payload, "payloadType": "InlineBase64", } ], } + if format == 'ipynb': + definition_payload["format"] = "ipynb" + create_item( name=name, type="Notebook", @@ -206,7 +226,10 @@ def create_notebook( def update_notebook_definition( - name: str, notebook_content: str, workspace: Optional[str | UUID] = None + name: str, + notebook_content: str, + workspace: Optional[str | UUID] = None, + format: Optional[str] = None ): """ Updates an existing notebook with a new definition. @@ -221,10 +244,13 @@ def update_notebook_definition( The name or ID of the workspace. Defaults to None which resolves to the workspace of the attached lakehouse or if no lakehouse attached, resolves to the workspace of the notebook. + format : str, default=None + If 'ipynb' is provided than notebook_content should be standard ipynb format + otherwise notebook_content should be GIT friendly format """ (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) - notebook_payload = base64.b64encode(notebook_content) + notebook_payload = base64.b64encode(notebook_content.encode("utf-8")).decode("utf-8") item_id = resolve_item_id(item=name, type="Notebook", workspace=workspace) type = _get_notebook_type(notebook_name=name, workspace=workspace) @@ -232,7 +258,7 @@ def update_notebook_definition( "definition": { "parts": [ { - "path": f"{_notebook_prefix}.{type}", + "path": f"{_notebook_prefix}{type}", "payload": notebook_payload, "payloadType": "InlineBase64", } @@ -240,6 +266,9 @@ def update_notebook_definition( }, } + if format == 'ipynb': + payload["definition"]["format"] = "ipynb" + _base_api( request=f"v1/workspaces/{workspace_id}/notebooks/{item_id}/updateDefinition", payload=payload, From 0493851694d83e0de4b331462ff4f404bdce1517 Mon Sep 17 00:00:00 2001 From: Lulzim Bilali Date: Mon, 12 May 2025 12:42:20 +0200 Subject: [PATCH 2/2] code simplification as pointed out during code review --- src/sempy_labs/_notebooks.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/sempy_labs/_notebooks.py b/src/sempy_labs/_notebooks.py index 2fd3c935..1e1d1fbb 100644 --- a/src/sempy_labs/_notebooks.py +++ b/src/sempy_labs/_notebooks.py @@ -28,13 +28,12 @@ def _get_notebook_definition_base( (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) item_id = resolve_item_id(item=notebook_name, type="Notebook", workspace=workspace) + url = f"v1/workspaces/{workspace_id}/notebooks/{item_id}/getDefinition" if format == 'ipynb': - format = f"?format={format}" - else: - format = '' - + url += f"?format={format}" + result = _base_api( - request=f"v1/workspaces/{workspace_id}/notebooks/{item_id}/getDefinition{format}", + request=url, method="post", lro_return_json=True, status_codes=None,