From 55c5cf1ddea0c323bdda0b61ea5e2a50390c185a Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 12 Dec 2024 12:07:06 +0200 Subject: [PATCH 1/2] enabling multiple notebook suffixes --- src/sempy_labs/_notebooks.py | 56 +++++++++++++++++++++++++++--------- src/sempy_labs/tom/_model.py | 8 ++++-- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/sempy_labs/_notebooks.py b/src/sempy_labs/_notebooks.py index b9d24222..41fc5950 100644 --- a/src/sempy_labs/_notebooks.py +++ b/src/sempy_labs/_notebooks.py @@ -10,6 +10,40 @@ _decode_b64, ) from sempy.fabric.exceptions import FabricHTTPException +import os + + +def _get_notebook_definition_base( + notebook_name: str, workspace: Optional[str] = None +) -> pd.DataFrame: + + (workspace, workspace_id) = resolve_workspace_name_and_id(workspace) + item_id = fabric.resolve_item_id( + item_name=notebook_name, type="Notebook", workspace=workspace + ) + client = fabric.FabricRestClient() + response = client.post( + f"v1/workspaces/{workspace_id}/notebooks/{item_id}/getDefinition", + ) + + result = lro(client, response).json() + + return pd.json_normalize(result["definition"]["parts"]) + + +def _get_notebook_type(notebook_name: str, workspace: Optional[str] = None) -> str: + + df_items = _get_notebook_definition_base( + notebook_name=notebook_name, workspace=workspace + ) + + file_path = df_items[df_items["path"].str.startswith("notebook-content")][ + "path" + ].iloc[0] + + _, file_extension = os.path.splitext(file_path) + + return file_extension[1:] def get_notebook_definition( @@ -38,17 +72,9 @@ def get_notebook_definition( The notebook definition. """ - (workspace, workspace_id) = resolve_workspace_name_and_id(workspace) - item_id = fabric.resolve_item_id( - item_name=notebook_name, type="Notebook", workspace=workspace + df_items = _get_notebook_definition_base( + notebook_name=notebook_name, workspace=workspace ) - client = fabric.FabricRestClient() - response = client.post( - f"v1/workspaces/{workspace_id}/notebooks/{item_id}/getDefinition", - ) - - result = lro(client, response).json() - df_items = pd.json_normalize(result["definition"]["parts"]) df_items_filt = df_items[df_items["path"] == "notebook-content.py"] payload = df_items_filt["payload"].iloc[0] @@ -127,6 +153,7 @@ def import_notebook_from_web( def create_notebook( name: str, notebook_content: str, + type: str = "py", description: Optional[str] = None, workspace: Optional[str] = None, ): @@ -139,6 +166,8 @@ def create_notebook( The name of the notebook to be created. notebook_content : str The Jupyter notebook content (not in Base64 format). + type : str, default="py" + The notebook type. description : str, default=None The description of the notebook. Defaults to None which does not place a description. @@ -158,7 +187,7 @@ def create_notebook( "format": "ipynb", "parts": [ { - "path": "notebook-content.py", + "path": f"notebook-content.{type}", "payload": notebook_payload, "payloadType": "InlineBase64", } @@ -202,13 +231,14 @@ def update_notebook_definition( item_name=name, type="Notebook", workspace=workspace ) + type = _get_notebook_type(notebook_name=name, workspace=workspace_id) + request_body = { "displayName": name, "definition": { - "format": "ipynb", "parts": [ { - "path": "notebook-content.py", + "path": f"notebook-content.{type}", "payload": notebook_payload, "payloadType": "InlineBase64", } diff --git a/src/sempy_labs/tom/_model.py b/src/sempy_labs/tom/_model.py index da36060b..d5ff4d00 100644 --- a/src/sempy_labs/tom/_model.py +++ b/src/sempy_labs/tom/_model.py @@ -4541,9 +4541,13 @@ def add_role_member(self, role_name: str, member: str | List[str]): rm.IdentityProvider = "AzureAD" rm.MemberName = m role.Members.Add(rm) - print(f"{icons.green_dot} '{m}' has been added as a member of the '{role_name}' role.") + print( + f"{icons.green_dot} '{m}' has been added as a member of the '{role_name}' role." + ) else: - print(f"{icons.yellow_dot} '{m}' is already a member in the '{role_name}' role.") + print( + f"{icons.yellow_dot} '{m}' is already a member in the '{role_name}' role." + ) def close(self): From 6134d73aefda930999f914e8cd262cbbfc8d0e56 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 12 Dec 2024 13:51:09 +0200 Subject: [PATCH 2/2] updated --- src/sempy_labs/_notebooks.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/sempy_labs/_notebooks.py b/src/sempy_labs/_notebooks.py index 41fc5950..f06ccded 100644 --- a/src/sempy_labs/_notebooks.py +++ b/src/sempy_labs/_notebooks.py @@ -12,6 +12,8 @@ from sempy.fabric.exceptions import FabricHTTPException import os +_notebook_prefix = "notebook-content." + def _get_notebook_definition_base( notebook_name: str, workspace: Optional[str] = None @@ -37,7 +39,7 @@ def _get_notebook_type(notebook_name: str, workspace: Optional[str] = None) -> s notebook_name=notebook_name, workspace=workspace ) - file_path = df_items[df_items["path"].str.startswith("notebook-content")][ + file_path = df_items[df_items["path"].str.startswith(_notebook_prefix)][ "path" ].iloc[0] @@ -75,7 +77,7 @@ def get_notebook_definition( df_items = _get_notebook_definition_base( notebook_name=notebook_name, workspace=workspace ) - df_items_filt = df_items[df_items["path"] == "notebook-content.py"] + df_items_filt = df_items[df_items["path"].str.startswith(_notebook_prefix)] payload = df_items_filt["payload"].iloc[0] if decode: @@ -187,7 +189,7 @@ def create_notebook( "format": "ipynb", "parts": [ { - "path": f"notebook-content.{type}", + "path": f"{_notebook_prefix}{type}", "payload": notebook_payload, "payloadType": "InlineBase64", } @@ -238,7 +240,7 @@ def update_notebook_definition( "definition": { "parts": [ { - "path": f"notebook-content.{type}", + "path": f"{_notebook_prefix}{type}", "payload": notebook_payload, "payloadType": "InlineBase64", }