We can embed Power BI reports in Jupyter notebooks, including notebooks crated in Microsoft Fabric. Let's see how!
Power BI Python Client
This solution is enabled by using the Microsoft Python powerbiclient
library within a Fabric Jupyter notebook. This library has many other features, so do review it in its GitHub repo, but in this post we'll just do a basic report embedding within a workbook to see how to access a Power BI report from Python code.
The demo in this post is available as a YouTube video demo, so if you'd rather see this done than read about it, click the YouTube embedded video link instead!
Review the Report
The Power BI report used in this demo is a sample report built on the Contoso sample database. Viewing the report in Fabric, we can see how it looks, and interact with its individual components in the browser.
Create a New Notebook
Switching to the Data Science experience, we can create a new Jupyter Notebook by tapping the Notebook tile.
A new, empty Jupyter notebook is created, with some placeholder comments in the first cell.
To access the Power BI content, we first install the Python dependency powerbiclient
using pip
.
Next, we import needed dependencies from powerbiclient
:
- Report. In the end, we'll create a
Report
object that reads the Power BI content for display in the notebook. - DeviceCodeLoginAuthentication. This object is used to obtain an access token allowing the notebook to access Power BI content in Fabric.
Authentication
To access Power BI content, we need an access token. In Fabric, this is done by calling DeviceCodeLoginAuthentication()
to begin an OAuth authentication flow.
The OAuth flow will prompt us to enter the device code provided in the Jupyter notebook browser window.
After entering the code, and confirming our Microsoft ID, we grant access to the Jupyter notebook so it can access the Power BI content.
After completing OAuth, we can create a new Report
object, providing the Group, Report and Device Auth Token.
Finally, we can view the report within the Jupyter notebook!