microsoft / semantic-link-labs

Early access to new features for Microsoft Fabric's Semantic Link.
MIT License
116 stars 20 forks source link

Download Paginated Report as a Function #36

Open jugi92 opened 2 months ago

jugi92 commented 2 months ago

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.

m-kovalsky commented 2 months ago

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 GitHubhttps://github.com/microsoft/semantic-link-labs/issues/36, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AHBQBNXSTSB3IJGIUZKIZH3ZM7RZTAVCNFSM6AAAAABLC7RCN6VHI2DSMVQWIX3LMV43ASLTON2WKOZSGQYTMOBRHEZDOMY. You are receiving this because you are subscribed to this thread.Message ID: @.***>

jugi92 commented 1 month ago

Yes, I tried it and created two PRs to enhance the functionality: https://github.com/microsoft/semantic-link-labs/pull/65 https://github.com/microsoft/semantic-link-labs/pull/67