growclip
growclip

Reputation: 105

Is it possible to run bokeh widget python callbacks in jupyter notebook

I'm working in the jupyter notebook. Is it possible to run python callbacks from a bokeh widget?

Upvotes: 6

Views: 1639

Answers (1)

bigreddot
bigreddot

Reputation: 34628

Yes, you can embed a bokeh server app in a Jupyter notebook by defining a function that modifies a Bokeh document, and passes it to show, e.g.:

def modify_doc(doc):
    df = sea_surface_temperature.copy()
    source = ColumnDataSource(data=df)

    plot = figure(x_axis_type='datetime', y_range=(0, 25),
                  y_axis_label='Temperature (Celsius)',
                  title="Sea Surface Temperature at 43.18, -70.43")
    plot.line('time', 'temperature', source=source)

    def callback(attr, old, new):
        if new == 0:
            data = df
        else:
            data = df.rolling('{0}D'.format(new)).mean()
        source.data = ColumnDataSource(data=data).data

    slider = Slider(start=0, end=30, value=0, step=1, title="Smoothing by N Days")
    slider.on_change('value', callback)

    doc.add_root(column(slider, plot))

You can see a complete example here:

https://github.com/bokeh/bokeh/blob/HEAD/examples/server/api/notebook_embed.ipynb

(You will need to run the notebook locally)

Upvotes: 4

Related Questions