Skip to content
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

enabling multiple notebook suffixes #348

Merged
merged 2 commits into from
Dec 13, 2024
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
60 changes: 46 additions & 14 deletions src/sempy_labs/_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,42 @@
_decode_b64,
)
from sempy.fabric.exceptions import FabricHTTPException
import os

_notebook_prefix = "notebook-content."


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_prefix)][
"path"
].iloc[0]

_, file_extension = os.path.splitext(file_path)

return file_extension[1:]


def get_notebook_definition(
Expand Down Expand Up @@ -38,18 +74,10 @@ 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"]
df_items_filt = df_items[df_items["path"].str.startswith(_notebook_prefix)]
payload = df_items_filt["payload"].iloc[0]

if decode:
Expand Down Expand Up @@ -127,6 +155,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,
):
Expand All @@ -139,6 +168,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.
Expand All @@ -158,7 +189,7 @@ def create_notebook(
"format": "ipynb",
"parts": [
{
"path": "notebook-content.py",
"path": f"{_notebook_prefix}{type}",
"payload": notebook_payload,
"payloadType": "InlineBase64",
}
Expand Down Expand Up @@ -202,13 +233,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_prefix}{type}",
"payload": notebook_payload,
"payloadType": "InlineBase64",
}
Expand Down
8 changes: 6 additions & 2 deletions src/sempy_labs/tom/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
Loading