2017-08-12 2 views
1

Ich möchte programmgesteuert von einem Jupyter Notebook überprüfen, ob ipywidgets aktiviert ist. Welche Wege gibt es? Ich habe versucht, in nbextensions und notebook Module zu suchen, fand aber keine Funktion, um aktivierte Erweiterungen aufzulisten.Wie überprüft man, ob eine Jupyter Notebook Erweiterung aktiviert ist?

Mein Notebook ist auf GitHub, und ich möchte dort eine statische Plot und zusätzlich eine interaktive Version, wenn der Benutzer tatsächlich das Notebook betreibt und ipywidgets installiert und aktiviert hat.

Der Plotten Code ist

# 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 Pseudo-Code, würde ich so etwas wie dies erreichen mag:

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

in der Befehlszeile, vom Konzept her ähnlich Befehl ist jupyter nbextension list, aber ich möchte tun Dies aus einer laufenden Python-Umgebung, bieten auch eine statische Plot, wenn der Benutzer nur auf GitHub (oder ähnlich) auf das Notebook schaut.

Danke

+0

Wie wäre es, die Handlung in jedem Fall zu zeigen und nur das Widget zu zeigen, wenn es möglich ist? – ImportanceOfBeingErnest

+0

Dank @ImportanceOfBeingErnest, konzeptionell genau das ist, was ich im Sinn habe. Da ich jedoch das Diagramm aus dem Widget neu zeichne, führt das zu zwei Kopien des gezeichneten Diagramms, wenn ipywidgets aktiviert ist: zuerst das statische und dann das interaktive. Das ist die Situation, die ich gerade habe :) –

Antwort

1

:) ich falsch verstehen kann, was genau hier gefordert wird; aber ich fühle mich wie eine normale try - except Lösung würde funktionieren.

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() 
Verwandte Themen