updating bokeh plot with a bokeh widget in jupyter notebook

后端 未结 1 697
不思量自难忘°
不思量自难忘° 2021-01-13 13:27

I want to use bokeh widgets from within a jupyter notebook to update a bokeh plot. My (somewhat hacky) code looks like this:

from bokeh.plotting import figur         


        
相关标签:
1条回答
  • 2021-01-13 13:43

    I got the plot to update as expected by displaying the figure and the slider widget within a bokeh.layouts.row layout:

    from bokeh.plotting import figure
    from bokeh.io import output_notebook, push_notebook, show
    from bokeh.models import CustomJS, Slider
    from bokeh.layouts import row
    
    output_notebook()
    
    power = 0.5
    x = [1,2,3]
    y = [i**power for i in x]
    
    fig = figure()
    plt = fig.circle(x, y)
    
    def update_plot(power):
        x = plt.data_source.data['x']
        plt.data_source.data['y'] = [i**power for i in x]
        push_notebook(handle=bokeh_handle)  
    
    
    ##### new notebook cell #####
    
    callback = CustomJS(code="""
    if (IPython.notebook.kernel !== undefined) {
        var kernel = IPython.notebook.kernel;
        cmd = "update_plot(" + cb_obj.value + ")";
        kernel.execute(cmd, {}, {});
    }
    """)
    
    slider = Slider(start=0.1, 
                    end=1,
                    value=1,
                    step=.05,
                    title="power",
                    callback=callback)
    bokeh_handle = show(row(fig, slider), notebook_handle=True)
    
    0 讨论(0)
提交回复
热议问题