Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 761
growclip Avatar asked Oct 18 '22 10:10

growclip


1 Answers

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/master/examples/howto/server_embed/notebook_embed.ipynb

(You will need to run the notebook locally)

like image 140
bigreddot Avatar answered Oct 21 '22 08:10

bigreddot