Skip to content

Commit 0ccf45a

Browse files
committed
added export_report
1 parent a223762 commit 0ccf45a

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import sempy.fabric as fabric
2+
import sempy_labs._icons as icons
3+
from typing import Optional
4+
from sempy_labs._helper_functions import (
5+
resolve_workspace_name_and_id,
6+
)
7+
from sempy_labs.lakehouse._lakehouse import lakehouse_attached
8+
from sempy.fabric.exceptions import FabricHTTPException
9+
10+
11+
def export_report(
12+
report: str,
13+
file_name: Optional[str] = None,
14+
download_type: str = "LiveConnect",
15+
workspace: Optional[str] = None,
16+
):
17+
"""
18+
Exports the specified report from the specified workspace to a Power BI .pbix file.
19+
20+
This is a wrapper function for the following API: `Reports - Export Report In Group <https://learn.microsoft.com/rest/api/power-bi/reports/export-report-in-group>`.
21+
22+
Parameters
23+
----------
24+
report: str
25+
Name of the report.
26+
file_name : str, default=None
27+
Name of the .pbix file to be saved.
28+
Defaults to None which resolves to the name of the report.
29+
download_type : str, default="LiveConnect"
30+
The type of download. Valid values are "LiveConnect" and "IncludeModel".
31+
workspace : str, default=None
32+
The Fabric workspace name.
33+
Defaults to None which resolves to the workspace of the attached lakehouse
34+
or if no lakehouse attached, resolves to the workspace of the notebook.
35+
"""
36+
37+
if not lakehouse_attached():
38+
raise ValueError(
39+
f"{icons.red_dot} A lakehouse must be attached to the notebook."
40+
)
41+
42+
download_types = ["LiveConnect", "IncludeModel"]
43+
if download_type not in download_types:
44+
raise ValueError(
45+
f"{icons.red_dot} Invalid download_type parameter. Valid options: {download_types}."
46+
)
47+
48+
file_name = file_name or report
49+
(workspace, workspace_id) = resolve_workspace_name_and_id(workspace)
50+
report_id = fabric.resolve_item_id(
51+
item_name=report, type="Report", workspace=workspace
52+
)
53+
54+
client = fabric.PowerBIRestClient()
55+
response = client.get(
56+
f"/v1.0/myorg/groups/{workspace_id}/reports/{report_id}/Export?downloadType={download_type}"
57+
)
58+
59+
if response.status_code != 200:
60+
raise FabricHTTPException(response)
61+
62+
# Save file to the attached lakehouse
63+
with open(f"/lakehouse/default/Files/{file_name}.pbix", "w") as file:
64+
file.write(response.text)
65+
66+
print(
67+
f"{icons.green_dot} The '{report}' report within the '{workspace}' workspace has been exported to the attached lakehouse as the '{file_name}' file."
68+
)

0 commit comments

Comments
 (0)