Search code examples
jupyter-notebookipywidgetsbqplot

bqplot: ipython widgets not being rendered


I am trying to use bqplot to plot some graphs in jupyter notebook along with some ipywidgets. I want to render the widgets horizontally adjacent to my plot as discussed in this issue, but I cannot get the widgets to show up in my jupyter notebook.

My code is as follows -

from bqplot import pyplot as plt
import ipywidgets as widgets
from pandas import DataFrame

class AdderDOEProblem_PlotUtils:
    def __init__(self, parseutils):
        self.data = DataFrame({'timestamps': parseutils.getTimestampValues(),
                            'Adder.sum': parseutils.getValues('Adder.sum'),
                            'Adder.a': parseutils.getValues('desvar_a.a'),
                            'Adder.b': parseutils.getValues('desvar_b.b')})
        # step size
        self.Adder_a__step = 0.1
        self.Adder_b__step = 0.1

        # axes configuration
        x_axis_values = self.data['Adder.a']
        y_axis_values = self.data['Adder.sum']

        self.fig = plt.figure(title='AdderDOEProblem')
        self.p = plt.plot(x_axis_values, y_axis_values)

        w_a_slider = widgets.FloatSlider(value=0, min=0, max=1, step=self.Adder_a__step, description='Adder.a')
        w_b_slider = widgets.FloatSlider(value=0, min=0, max=1, step=self.Adder_b__step, description='Adder.b')
        self.widgets_list = [w_a_slider, w_b_slider]

    def update (self, change):
        # Placeholder logic for testing 
        self.p.y = [i+1 for i in self.p.y]

    def plot (self):
        plt.show()
        for w in self.widgets_list:
            w.observe(self.update, 'value')
        self.update(None)
        widgets.HBox([widgets.VBox(self.widgets_list), self.fig])

When I run it in the notebook, I get the following output -

jupyter notebook output

I have tried the following command as suggested in many of the threads (but with no luck) -

jupyter nbextension enable --py widgetsnbextension

What am I missing?

P.S. The versions of the packages are as follows -
jupyter - 1.0.0
ipython - 5.1.0
ipywidgets - 5.2.2
bqplot - 0.8.4


Solution

  • In your plot function, you have to return the HBox which is being created. That is why it is not being displayed. The figure which is displayed comes from the plt.show command, which only shows the Figure in the current context. Notice that you don't need the plt.show() anymore. So the plot function would look like,

    def plot (self):
        for w in self.widgets_list:
            w.observe(self.update, 'value')
        self.update(None)
        return widgets.HBox([widgets.VBox(self.widgets_list), self.fig])