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 :)
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()
!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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With