Skip to content

Add format parameter when receiving/creating or updating notebook definitions #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/sempy_labs/_helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,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."
)


Expand Down
48 changes: 38 additions & 10 deletions src/sempy_labs/_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@


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)

url = f"v1/workspaces/{workspace_id}/notebooks/{item_id}/getDefinition"
if format == 'ipynb':
url += f"?format={format}"

result = _base_api(
request=f"v1/workspaces/{workspace_id}/notebooks/{item_id}/getDefinition",
request=url,
method="post",
lro_return_json=True,
status_codes=None,
Expand All @@ -53,7 +60,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.
Expand All @@ -71,6 +81,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
-------
Expand All @@ -79,7 +92,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]
Expand Down Expand Up @@ -163,6 +176,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.
Expand All @@ -182,20 +196,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",
Expand All @@ -206,7 +225,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.
Expand All @@ -221,25 +243,31 @@ 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)

payload = {
"definition": {
"parts": [
{
"path": f"{_notebook_prefix}.{type}",
"path": f"{_notebook_prefix}{type}",
"payload": notebook_payload,
"payloadType": "InlineBase64",
}
],
},
}

if format == 'ipynb':
payload["definition"]["format"] = "ipynb"

_base_api(
request=f"v1/workspaces/{workspace_id}/notebooks/{item_id}/updateDefinition",
payload=payload,
Expand Down