The Jupyter Notebook is not included with Python, so if you want to try it out, you will need to install Jupyter. There are many distributions of the Python language. This article will focus on just two of them for the purposes of installing Jupyter Notebook. Jupyter Notebook is built off of IPython, an interactive way of running Python code in the terminal using the REPL model (Read-Eval-Print-Loop). The IPython Kernel runs the computations and communicates with the Jupyter Notebook front-end interface. It also allows Jupyter Notebook to support multiple languages. Jupyter notebooks are one way engineers can write and execute Python code. Jupyter notebooks contain Python code, the output of that code produces when it is run and markdown cells to explain what the code means. A Jupyter notebook can be started from the Anaconda Prompt, the Windows start menu or by using the Anaconda Navigator.minutes
Many people use Python with notebooks, so let’s take a look at one possible way to enable a Jupyter external tool for Power BI Desktop. The following stepwise approach begins with simply opening Jupyter. It then progresses to creating and opening a notebook that includes Power BI’s server and database arguments. Finally, it works its way toward downloading a notebook definition contained in a GitHub gist and connects to Power BI’s tabular model to start to make this approach more useful.
This post continues a series of posts related to Python and Power BI. The first three parts of this blog series introduced some possible uses for Python connected to a Power BI model, how to setup a basic Python external tool, and how to both use it with a virtual environment and connect to the Tabular Object Model.
Open Jupyter: Foundational But Not Useful
I use the Anaconda distribution in these examples, and let’s consider how to simply start and open a notebook (vanilla Jupyter and nothing with JupyterLab). We only need a pbitool.json file at this stage because the goal is simply launch Jupyter–not even a notebook file. It’s not a useful example but provides a foundation on which we’ll build.
As discussed in Part 2, pbitool.json files define Power BI Desktop’s external tools. If you look at the Jupyter shortcut, it provides the inspiration for how to define Power BI’s external tool.
The JSON in this gist would need to be saved as a [tool].pbitool.json file and placed in C:Program Files (x86)Common FilesMicrosoft SharedPower BI DesktopExternal Tools.
Note the path is for Python.exe, and arguments load the default Python environment and run the jupyter-notebook-script.py script to launch Jupyter.
There’s no value in simply opening Jupyter though. At this stage, it’s nothing more than a replacement for a shortcut. The real value is integrating Power BI Desktop more closely with the notebook.
Create and Open a Notebook (Hardcoded)
Taking it a step further, let’s create an empty notebook and write the currently open Power BI Desktop connection information to cells in the notebook. In this case, we need a Python script along with a new [tool].pbitool.json file.
The [tool].pbitool.json file has a few differences from the previous version. Most notably, it executes a Python script that I’ve named powerbinotebook.pyand placed in my C:Temp directory. You’ll also see the server and database arguments that Power BI Desktop uses to pass the current local connection (server:port and database GUID).
C:/Temp/powerbinotebook.py ”%server%” ”%database%”
The rest of the arguments start the default Python environment and launch Jupyter, but it’s important that the powerbinotebook.py script runs before Jupyter launches. Why is that?
Jupyter Notebook Python Online
The Python script defines a nearly empty notebook for Jupyter and then launches it in Jupyter. Pay close attention to notebook_core, which contains the JSON definition for the notebook and writes sys.argv and sys.argv into the notebook for the Power BI server and database respectively. Each cell in the cells array will appear as a separate cell in the sample notebook. As required by the pbitool.json definition above, the Python script is located in C:Temppowerbinotebook.py, and you could obviously name and save your own to your location of choice.
The following video shows the basic script execution, where the Python script creates the notebook, launches Jupyter, and Jupyter launches the notebook.
Create and Open a Notebook Defined in a GitHub Gist
Taking it a step further, let’s shift the notebook definition to a GitHub gist instead of hardcoding it into the Python script. The gist contains the JSON and also placeholders for <<PowerBIServer>> and <<PowerBIDatabase>>, which the Python script will replace with the actual sys.argv and sys.argv values.
Sample gist (not embedded in the post): https://gist.github.com/deldersveld/fdada06b82621233d6e40fac13cceb55
I used the same [tool].pbitool.json file since only the contents of the powerbinotebook.py script change. Note how the script is much shorter now that it no longer contains the notebook definition. This uses the requests library to get the JSON notebook template from the URL defined in gist_url. Note as well how replace swaps in the current values of the Power BI Server and Database.
Extending the Notebook Definition
Finally, let’s build a slightly more complex notebook using an updated powerbinotebook.py script. As before, no modification is required for [tool].pbitool.json. Only the Python script updates.
Here is the updated notebook Gist for reference (not embedded in post):
As with the last example, the Python script downloads the new notebook definition contained in a different GitHub gist. Also as before, it replaces the <<PowerBIServer>> and <<PowerBIDatabase>> placeholders with the actual values passed into sys.argv from Power BI Desktop.
What’s new here is that this script downloads the ssas_api.pymodule mentioned in the Part 3 post, which is one among many possible ways to connect Python to AMO / TOM and other .NET libraries. The updated sample notebook loads the module and demonstrates basic model connectivity.
NOTE that the script intentionally does not overwrite a notebook once it’s created for a specific port number. To get a new blank template if you are testing this out simply requires closing and re-opening Power BI Desktop. Connecting to TOM in this sample also requires a Power BI Desktop connection with at least one existing table.
Here’s a look at the sample notebook in action. It illustrates two things: a basic connection to TOM, and the evaluation of a DAX query against the model open in Power BI Desktop.
With a live connection to the Power BI Desktop model from Python, you can unlock a lot of possibilities. As described in Part 1, there are many examples ranging from data visualization to updating the model. I’m curious to hear your thoughts on how you might want to use Python in Power BI.
Let me know in the comments, and don’t forget to subscribe to the DataVeld blog.
As discussed in the introduction, Jupyter notebooks provide a tremendous advantage for scientific computing, analysis and visualization. Here, we will explain how to start a Jupyter notebook. We will present just enough information to get you started. For a more complete treatment of Jupyter notebook software, please read the official documentation.
In the previous section, we explained how to download and install a complete Python installation with Anaconda for both Mac OS X and Windows. Armed with this experience, we will demonstrate how to start a Jupyter notebook in order to run code.
The Jupyter Notebook software is included in the Python installation we obtained from Anaconda. There are at least two scenarios in which you may want to run a Jupyter notebook:
- You aim to further experiment with, or augment, an existing Jupyter notebook like the ones that already exist within a repository in github.
- You want to develop a Jupyter notebook or series of notebooks for uses such as supplementing teaching material, or for a scholarly journal article, for example.
In both cases, you will want to start the Jupyter notebook in a specific folder.
Starting a Jupyter Notebook in a Specific Folder
On Mac OS X, create a Desktop folder with the Finder called
my-notebook. In the last section, we learned how to start the OS X Terminal. Launch the Terminal again so that we may start Jupyter. Change to that directory (i.e., open the newly created
my-notebook folder) with the
cd ~/Desktop/my-notebook. The
~ character is a shortcut to indicate you are at your home directory.
On Windows, create a Desktop folder called
my-notebook. Hold down the shift key and right-click on the newly created folder. A context menu will appear and there will be an option to, 'Open command window here'. Click this option to launch the Windows Command Prompt at that specific location.
We can now start our Jupyter notebook with this command:
Once entered, this command will automatically launch your default web browser with a new notebook in an empty directory.
To actually create a notebook, go to the New, Python 3 menu item.
Enter some code in the first cell:
Jupyter Notebook Python 3
and press Shift-Enter to actually run the cell yielding
For more information on the basics of using the Jupyter Notebook, we recommend starting with this notebook on Notebook Basics.
How to Run OPT Jupyter Notebooks
This project includes a series of Jupyter notebooks for learning Python with geoscience objectives. They are located in the
notebooks folder in the git
master branch. In order to clone this project or any projects containing Jupyter notebooks such as the Unidata Python Workshop, please see the section on git and github. Once you have cloned your repository, change directory to where the Jupyter notebooks are located in the repository folder (e.g., the
notebooks directory) and start Jupyter with the
jupyter notebook command.
MyBinder and Starting your Jupyter Notebook in the Cloud
Change Jupyter Notebook Python Version
MyBinder is a third option that runs Jupyter notebooks on a cloud server that is setup on your behalf. If the project has enabled MyBinder, you will see a badge on the github project page. MyBinder is available for the Unidata Python Workshop Jupyter notebooks at this link. See the MyBinder website for more information.