-
Notifications
You must be signed in to change notification settings - Fork 55
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
Download Paginated Report as a Function #36
Labels
enhancement
New feature or request
Comments
Have you tried this function?
https://semantic-link-labs.readthedocs.io/en/stable/sempy_labs.report.html#sempy_labs.report.export_report
…________________________________
From: jugi92 ***@***.***>
Sent: Thursday, July 18, 2024 7:06:49 PM
To: microsoft/semantic-link-labs ***@***.***>
Cc: Subscribed ***@***.***>
Subject: [microsoft/semantic-link-labs] Download Paginated Report as a Function (Issue #36)
Problem
Running and exporting paginated reports into Excel and PDFs via Notebook
Solution
A simple function to run the report and specify the output path.
Example code to be integrated into Sempy:
def download_paginated_report(group_id:str, report_id:str, file_format:str="PDF", target_folder="/lakehouse/default/Files/", filename="report.pdf", report_parameters:dict={}):
import requests
import json
import time
import os
token = mssparkutils.credentials.getToken('pbi')
headers = {
'Authorization': f'Bearer {token}',
'Content-type': 'application/json'
}
payload = {
"format": file_format
}
url = f"https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/ExportTo?"
for key, value in report_parameters.items():
url = url + f"rp:{key}={value}&"
print(url)
export_response = requests.post(url, data=json.dumps(payload), headers=headers)
export_response.raise_for_status()
export_id = export_response.json()["id"]
timeout = 60
counter = 0
wait_time = 5
while True:
url = f"https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/exports/{export_id}"
status_response = requests.get(url, headers=headers)
if status_response.json()["status"] == "Succeeded":
break
if counter > timeout:
raise(TimeoutError("Request timed out"))
counter = counter + wait_time
time.sleep(wait_time)
url = f"https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/exports/{export_id}/file"
report_response = requests.get(url, headers=headers)
report_response.raise_for_status()
os.chdir(target_folder)
with open(filename, 'wb') as f:
f.write(report_response.content)
print(f"export saved as {filename} in {target_folder}")
Alternatives
Keeping the above mentioned code in a notebook and maintaining it, but I would prefer to have it in semantic-link-labs to scale the usage.
—
Reply to this email directly, view it on GitHub<#36>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AHBQBNXSTSB3IJGIUZKIZH3ZM7RZTAVCNFSM6AAAAABLC7RCN6VHI2DSMVQWIX3LMV43ASLTON2WKOZSGQYTMOBRHEZDOMY>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem
Running and exporting paginated reports into Excel and PDFs via Notebook
Solution
A simple function to run the report and specify the output path.
Example code to be integrated into Sempy:
Alternatives
Keeping the above mentioned code in a notebook and maintaining it, but I would prefer to have it in semantic-link-labs to scale the usage.
The text was updated successfully, but these errors were encountered: