Skip to content

adding the ability to export a report definition to a notebook's built in resources #613

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 37 additions & 2 deletions src/sempy_labs/report/_generate_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pandas as pd
import json
import os
import base64
from typing import Optional
from sempy_labs._helper_functions import (
resolve_workspace_name_and_id,
Expand Down Expand Up @@ -182,6 +183,7 @@ def get_report_definition(
report: str | UUID,
workspace: Optional[str | UUID] = None,
return_dataframe: bool = True,
files_to_built_in_resources = False
) -> pd.DataFrame | dict:
"""
Gets the collection of definition files of a report.
Expand All @@ -198,17 +200,50 @@ def get_report_definition(
or if no lakehouse attached, resolves to the workspace of the notebook.
return_dataframe : bool, default=True
If True, returns a dataframe. If False, returns a json dictionary.
files_to_built_in_resources : bool, default=False
If True, saves the files to the built in resources of a fabric notebook so they can be downloaded. If False does nothing.

Returns
-------
pandas.DataFrame
The collection of report definition files within a pandas dataframe.
"""

return get_item_definition(
report_items = get_item_definition(
item=report, type="Report", workspace=workspace, return_dataframe=True
)

if files_to_built_in_resources:
output_base_dir = f"./builtin/{report}"
if os.path.exists(output_base_dir):
parent_directory = "./builtin/"
target_name = report

matching_items = [
item for item in os.listdir(parent_directory)
if item == target_name
]
count = len(matching_items)
new_folder_name = f"{output_base_dir} ({count})"
while os.path.exists(new_folder_name):
count += 1
new_folder_name = f"{output_base_dir} ({count})"
os.makedirs(new_folder_name, exist_ok=True)
output_base_dir = new_folder_name
else:
os.makedirs(output_base_dir, exist_ok=True)

for _, row in report_items.iterrows():
relative_path = row['path']
base64_payload = row['payload']
output_path = os.path.join(output_base_dir, relative_path)

os.makedirs(os.path.dirname(output_path), exist_ok=True)

with open(output_path, "wb") as f:
f.write(base64.b64decode(base64_payload))

return report_items


@log
def create_model_bpa_report(
Expand Down