January 1, 2022

How to Optimize Your Jupyter Notebooks with Embedded Power BI Reports

By Zac Zinda

As a Python user, one of the most time-consuming parts of any data science project I complete is producing clean, aesthetically pleasing visualizations throughout the exploratory data analysis process. While Python libraries like Matplotlib, Seaborn, and Plotly offer infinite flexibility, the standard visualizations often leave much to be desired. Searching through the documentation or StackOverflow to find the right code tweaks to make that unappealing graph ready for the eyes of a client or manager can eat up hours of precious time. 

On many occasions, I have found myself loading my data into Power BI to slice my way through the data exploration process in a fraction of the time. As of May of this year, Microsoft now allows me to take that work I’ve done in Power BI and embed it back into my Jupyter Notebook alongside all of the code that makes up the rest of my data science project.

How to Embed a Report into a Jupyter Notebook

In order to embed a report, it needs to be published in a workspace on the Power BI service. For my example, I downloaded the classic Iris dataset from the UCI Machine Learning repository and created a report with Power BI desktop. Using Power BI’s Small Multiples feature released this year, I can throw together a pretty decent-looking stacked histogram on a 2×2 grid in about 90 seconds and a half dozen clicks.

Now that I have a report, I’ll publish it to a workspace, and we’ll go through the steps to embed the report into a Jupyter Notebook. 

In order to work with Power BI within a notebook, we need to have the PowerBIClient library installed. This can be accomplished with a simple pip install.

After installing the library, we’ll import the Report and DeviceCodeLoginAuthentication classes from the Authentication module. 

Next, we need to authenticate our device. Running the piece of code below will produce a URL and code needed for authentication. Simply click the link and follow the instructions by entering the provided code and signing into Power BI. Text will appear in the cell output, letting you know authentication was successful.

Finally, we’ll need the workspace ID and report ID for the report we want to embed. We can find this by navigating to the Power BI service report and copying and pasting the group ID and report ID strings from the URL.

We’ll create variables with those text strings.

And finally, pass the group_id, report_id, and device_auth variables we’ve created as arguments for the Report() function. 

We now have all of the functionality of a Power BI report, complete with filters, tooltips, and the ability to navigate between any additional report pages we might create, right inside our Jupyter notebook in a fraction of the time it would have taken the average python user to write the code for the equivalent visuals.

Have more Power BI questions? Our team of Power BI experts are here to help!

Data Coach is our premium analytics training program with one-on-one coaching from renowned experts.

Accelerate and automate your data projects with the phData Toolkit