Using Plotly in Jupyter Notebooks on Microsoft Azure

Jupyter Notebooks provide an interactive computing workflow that is familiar to many scientists and engineers. Jupyter supports many programming languages including Python, R, Julia, MATLAB and over 50 more.  This allows developers to choose the right language for the job while working in a familiar interface and with a common document format for sharing their results.. You can read more about Jupyter Notebook on the project website.

In this post, we will learn about using the Microsoft Azure Notebooks service. It provides Jupyter notebook hosting, code execution, and out-of-the-box support for popular Python libraries.

Before we start, here are some of the restrictions of this free service.

  • Your code can access Azure, GitHub, PyPI, and CRAN.
  • The memory is limited to 4Gb.
  • Your data might get removed after 60 days of inactivity.

Getting your Notebook Server running

Go to https://notebooks.azure.com/ and click “Go to my Notebook Server.” You’ll be asked to sign into your Microsoft account, or you can create a free account if you do not have one.

Screen Shot 2016-08-08 at 10.46.21 PM

After you have authenticated and clicked on “Go to my Notebook Server,” you will see a list of all the Jupyter notebooks in your account. Here is what it looks like in my case:

Notebook Tree
Notebook Tree

 

To create a new Jupyter notebook, choose the programming language of your choice under the “New” menu. The service supports Python 2, Python 3, and R kernels. We will use Python 2 in this post.

Package installation

For the Python kernel, the Plotly package should be preinstalled automatically. Here is how importing Plotly’s Python package should look:

Screen Shot 2016-08-08 at 10.57.09 PM

For Python or R packages that are not automatically preinstalled in the MS Azure notebook environment, you can install them using this syntax:

Python

R

Configure Plotly for offline use

According to the limitations of the Microsoft Azure Notebooks service, our code can’t send the chart data to Plotly. That is why we will have to use Plotly in offline mode.

You will receive a ProxyError if you don’t use Plotly in offline mode.

Import the graph_objs module to plot the different Plotly graph objects.

The method init_notebook_mode injects the plotly.js source files into the notebook.

Import your data

On the Azure Notebook Service, we can access Azure, GitHub, PyPI, and CRAN using our code.

We will use the “Motor Trend Car Road Tests” dataset, hosted by Plotly on their GitHub.

import data file from GitHub using ‘wget’ command

You can execute the available bash commands by appending ! as a prefix.

This will download the csv data file with the name motor_trend_car_road_tests.csv and you can use it like any other file available to you.

import data file from github using ‘requests’ library

In this tutorial notebook, you can find other advanced methods for importing data from SQL, Azure Table Storage, etc.

charting with Plotly

Once you have the data required, you are ready to plot.

We assume that you have imported the Plotly package, then configured Plotly for offline mode as explained above.

Here, we are using the downloaded csv file (motor_trend_car_road_tests.csv) as a source to read the file as a pandas DataFrame.

We will have to import the FigureFactory module to use the create_scatterplotmatrix function.

Prepare the data and layout for the plot.

Similarly, you can create other types of plots.

Sharing your notebooks

We have created a notebook on the service for reference, here is the link.

Notebook Preview
Notebook Preview

 

You might have to share your notebook with your co-workers or students. These are the steps to share a notebook using the ‘library’ feature:

  1. Save the notebook on your machine using the menu bar option File | Download as | Notebook (.ipynb).
  2. Go to libraries.
  3. Create a new library (“+New Library”) and name it accordingly.
  4. Add one or more notebooks (“+Add New Notebook”) to the library.
  5. Share the library on social media OR with a link:
Create Library
Create Library
Add notebook to Library
Add notebook to Library