5

I would like to programmatically check from a Jupyter Notebook if ipywidgets is enabled. What ways are there to do so? I tried looking into nbextensions and notebook modules, but did not find a function to list enabled extensions.

My Notebook is on GitHub, and I want to provide a static plot there, and additionally also an interactive version if the user is actually running the notebook and has ipywidgets installed, and enabled.

The plotting code is

# Setup by importing some libraries and configuring a few things
import numpy as np
import ipywidgets
import matplotlib.pyplot as plt
%matplotlib inline

# Create two random datasets
data = np.random.random(15)
smallerdata = np.random.random(15) * 0.3

# Define a plotting function
def drawPlot():
    plt.plot(range(len(data)), data, label="random data");
    plt.plot(range(len(smallerdata)), smallerdata, 'r--', label="smaller random data");
    plt.title("Two random dataset compared");
    plt.grid(axis='y');
    plt.legend();

# Define an interactive annotation function
def updatePlot(s=0):
    print("data {0:.2f}, smallerdata {1:.2f}".format(data[s], smallerdata[s]))
    drawPlot()
    plt.annotate(s=round(data[s], 2),
                 xy=(s, data[s]),
                 xytext=(s + 2, 0.5),
                 arrowprops={'arrowstyle': '->'});
    plt.annotate(s=round(smallerdata[s], 2),
                 xy=(s, smallerdata[s]),
                 xytext=(s + 2, 0.3),
                 arrowprops={'arrowstyle': '->'});
    plt.show();

In pseudocode, I would like to achieve something like this:

if nbextensions.enabled('ipywidgets'):
    slider = ipywidgets.interactive(updatePlot, s=(0, len(data) - 1, 1));
    display(slider)
else:
    drawPlot()

From the command line, conceptually similar command is jupyter nbextension list, but I want to do this from a running Python environment, also also provide a static plot when the user is just looking at the Notebook on GitHub (or similar).

Thank you :)

Mace Ojala
  • 335
  • 4
  • 9
  • What about showing the plot in any case and just show the widget if it is possible? – ImportanceOfBeingErnest Aug 12 '17 at 19:26
  • Thanks @ImportanceOfBeingErnest , conceptually that is just what I have in mind. However since I am re-drawing the plot from the widget, that will lead to two copies of the plot being drawn if ipywidgets is enabled: first the static one, and then the interactive one. This is the situation I have right now :) – Mace Ojala Aug 12 '17 at 19:33

2 Answers2

3

I may misunderstand what exactly is demanded here; however, I feel like a usual try-except solution would work.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

data = np.random.random(15)
smallerdata = np.random.random(15) * 0.3

def drawPlot():
    plt.plot(range(len(data)), data, label="random data");
    plt.plot(range(len(smallerdata)), smallerdata, 'r--', label="smaller random data");
    plt.title("Two random dataset compared");
    plt.grid(axis='y');
    plt.legend();

def updatePlot(s=0):
    print("data {0:.2f}, smallerdata {1:.2f}".format(data[s], smallerdata[s]))
    drawPlot()
    plt.annotate(s=round(data[s], 2),
                 xy=(s, data[s]),
                 xytext=(s + 2, 0.5),
                 arrowprops={'arrowstyle': '->'});
    plt.annotate(s=round(smallerdata[s], 2),
                 xy=(s, smallerdata[s]),
                 xytext=(s + 2, 0.3),
                 arrowprops={'arrowstyle': '->'});
    plt.show();

try:
    import ipywidgets
    from IPython.display import display
    slider = ipywidgets.interactive(updatePlot, s=(0, len(data) - 1, 1));
    display(slider)
except:
    drawPlot()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
3
!jupyter nbextension list

would be a way to get a list of enable extensions from inside the notebook, you could get the output and see if the string are there, for instance

import subprocess
output = subprocess.getoutput('jupyter nbextension list')
if 'jupytext/index enabled' not in output:
    logging.error('jupytext is not active')
Michel Kluger
  • 164
  • 1
  • 6