We are trying to use the powerbiclient
package inside an Azure Databricks notebook to get information on reports but we are getting the error Exception: Power BI report is not embedded
.
The same code works instead if we use it locally on Visual Studio Code.
- Azure Databricks cluster: Personal Compute, runtime 15.3
- Packages of interest:
- powerbiclient==3.1.1
Here is the code we are using:
!pip install powerbiclient==3.1.1
dbutils.library.restartPython()
from powerbiclient import Report, models
from io import StringIO
from ipywidgets import interact
import requests
import json
We tried both authenticating via Device Code Login and Service Principal, but we need to stick with the second option:
# # option 1
# from powerbiclient.authentication import DeviceCodeLoginAuthentication
# device_auth = DeviceCodeLoginAuthentication()
# option 2
def azuread_auth(tenant_id: str, client_id: str, client_secret: str, resource_url: str):
"""
Authenticates Service Principal to the provided Resource URL, and returns the OAuth Access Token
"""
url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/token"
payload = f'grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}&resource={resource_url}'
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.request("POST", url, headers=headers, data=payload)
access_token = json.loads(response.text)['access_token']
return access_token
tenant_id = 'XXX'
client_id = 'YYY
client_secret = 'ZZZ'
scope = 'https://analysis.windows.net/powerbi/api/.default'
resource_url = 'https://analysis.windows.net/powerbi/api'
token = azuread_auth(tenant_id, client_id, client_secret, resource_url)
And then we call the report:
group_id = '123-456'
dataset_id = 'abc-def'
report_id = '7g8-h9i'
report = Report(group_id=group_id, report_id=report_id, auth=token)
But we see that it is not embedded:
print(report._embedded)
# False
If we try to display the report we obtain nothing:
def loaded_callback(event_details):
print('Report is loaded')
report.on('loaded', loaded_callback)
def rendered_callback(event_details):
print('Report is rendered')
report.on('rendered', rendered_callback)
report.set_size(200, 300)
report
And if we try to get the pages we get the aforementioned error:
pages = report.get_pages()
Exception Traceback (most recent call last)
File <command-2809091020085831>, line 3
1 report_dict = {}
2 # Get list of pages
----> 3 pages = report.get_pages()
4 for page in pages:
5 report.set_active_page(page['name'])
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-48b6b502-a176-4d52-a15d-9ed2a921ac04/lib/python3.11/site-packages/powerbiclient/report.py:566, in Report.get_pages(self)
560 """Returns pages list of the embedded Power BI report
561
562 Returns:
563 list: list of pages
564 """
565 if not self._embedded:
--> 566 raise Exception(self.REPORT_NOT_EMBEDDED_MESSAGE)
568 # Start getting pages on client side
569 self._get_pages_request = True
Exception: Power BI report is not embedded
Is there a way to embed PowerBI reports inside Azure Databricks notebooks?