Skip to content

Commit 747dedd

Browse files
committed
Merge branch 'm-kovalsky/notebooksuffix'
2 parents b985f9f + 6134d73 commit 747dedd

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

src/sempy_labs/_notebooks.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,42 @@
1010
_decode_b64,
1111
)
1212
from sempy.fabric.exceptions import FabricHTTPException
13+
import os
14+
15+
_notebook_prefix = "notebook-content."
16+
17+
18+
def _get_notebook_definition_base(
19+
notebook_name: str, workspace: Optional[str] = None
20+
) -> pd.DataFrame:
21+
22+
(workspace, workspace_id) = resolve_workspace_name_and_id(workspace)
23+
item_id = fabric.resolve_item_id(
24+
item_name=notebook_name, type="Notebook", workspace=workspace
25+
)
26+
client = fabric.FabricRestClient()
27+
response = client.post(
28+
f"v1/workspaces/{workspace_id}/notebooks/{item_id}/getDefinition",
29+
)
30+
31+
result = lro(client, response).json()
32+
33+
return pd.json_normalize(result["definition"]["parts"])
34+
35+
36+
def _get_notebook_type(notebook_name: str, workspace: Optional[str] = None) -> str:
37+
38+
df_items = _get_notebook_definition_base(
39+
notebook_name=notebook_name, workspace=workspace
40+
)
41+
42+
file_path = df_items[df_items["path"].str.startswith(_notebook_prefix)][
43+
"path"
44+
].iloc[0]
45+
46+
_, file_extension = os.path.splitext(file_path)
47+
48+
return file_extension[1:]
1349

1450

1551
def get_notebook_definition(
@@ -38,18 +74,10 @@ def get_notebook_definition(
3874
The notebook definition.
3975
"""
4076

41-
(workspace, workspace_id) = resolve_workspace_name_and_id(workspace)
42-
item_id = fabric.resolve_item_id(
43-
item_name=notebook_name, type="Notebook", workspace=workspace
77+
df_items = _get_notebook_definition_base(
78+
notebook_name=notebook_name, workspace=workspace
4479
)
45-
client = fabric.FabricRestClient()
46-
response = client.post(
47-
f"v1/workspaces/{workspace_id}/notebooks/{item_id}/getDefinition",
48-
)
49-
50-
result = lro(client, response).json()
51-
df_items = pd.json_normalize(result["definition"]["parts"])
52-
df_items_filt = df_items[df_items["path"] == "notebook-content.py"]
80+
df_items_filt = df_items[df_items["path"].str.startswith(_notebook_prefix)]
5381
payload = df_items_filt["payload"].iloc[0]
5482

5583
if decode:
@@ -127,6 +155,7 @@ def import_notebook_from_web(
127155
def create_notebook(
128156
name: str,
129157
notebook_content: str,
158+
type: str = "py",
130159
description: Optional[str] = None,
131160
workspace: Optional[str] = None,
132161
):
@@ -139,6 +168,8 @@ def create_notebook(
139168
The name of the notebook to be created.
140169
notebook_content : str
141170
The Jupyter notebook content (not in Base64 format).
171+
type : str, default="py"
172+
The notebook type.
142173
description : str, default=None
143174
The description of the notebook.
144175
Defaults to None which does not place a description.
@@ -158,7 +189,7 @@ def create_notebook(
158189
"format": "ipynb",
159190
"parts": [
160191
{
161-
"path": "notebook-content.py",
192+
"path": f"{_notebook_prefix}{type}",
162193
"payload": notebook_payload,
163194
"payloadType": "InlineBase64",
164195
}
@@ -202,13 +233,14 @@ def update_notebook_definition(
202233
item_name=name, type="Notebook", workspace=workspace
203234
)
204235

236+
type = _get_notebook_type(notebook_name=name, workspace=workspace_id)
237+
205238
request_body = {
206239
"displayName": name,
207240
"definition": {
208-
"format": "ipynb",
209241
"parts": [
210242
{
211-
"path": "notebook-content.py",
243+
"path": f"{_notebook_prefix}{type}",
212244
"payload": notebook_payload,
213245
"payloadType": "InlineBase64",
214246
}

0 commit comments

Comments
 (0)