Search code examples
javascriptpythonbokehbokehjs

Bokeh js change or rerender one plot to another plot by clicking


I have these python code

from bokeh.embed import components
from bokeh.plotting import figure
from bokeh.models import layouts, CustomJS, Select

bokeh_tools = "pan,wheel_zoom"
plot_1 = figure(x_axis_type="datetime", plot_width=800, tools=bokeh_tools, title="plot_1")
plot_1.line(x_values, y_values)
plot_2 = figure(x_axis_type="datetime", plot_width=800, tools=bokeh_tools, title="plot_2")
plot_2.line(x_values_other, y_values_other)
plot_3 = figure(x_axis_type="datetime", plot_width=800, tools=bokeh_tools, title="plot_3")
plot_3.line(x_values, y_values)

select = Select(title="SELECT", options=["val1", "val2"])
column = layouts.Column(plot_1, select, plot_2)

select.callback = CustomJS(args={"column": column,
                                 "plot_1": plot_1,
                                 "plot_2": plot_2,
                                 "plot_3": plot_3}, code="""

             if(cb_obj.value === "val1"){ 
               Bokeh.index[column.id].child_views[plot_2.id].remove(); // remove plot_2 from html
               //what i must to do to add generated on python side plot_3 and show it 
             }else if(cb_obj.value === "val2"){
              // some code here
             }""")

script, div = components(column)

Then this div and script i insert in html and show on the some page. On the page will visible 'plot_1', 'plot_2' and 'select' is dropdown. These plots have differents values with many lines. I want to click by selected dropdown and then change plot_2 on plot_3.

What i had to do that plot_3 render in html document by clicking on dropdown? Or any other way to change rerender plots by clicking on client html ?


Solution

  • You don't need to create a third plot, especially because it seems like both plot_2 and plot_3 are line plots with date-times on the x-axis. You can just go ahead and change the data in plot_2 according to your selection in the drop-down menu.

    One way to do this is to use Bokeh's ColumnDataSource (see also https://docs.bokeh.org/en/latest/docs/reference/models/sources.html and https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html#javascript-callbacks). You can create two of them: One with data for your plot_2, and another one with data that is currently supposed to show up in your plot_3. Then, in your callback, just change the source of your lines, which is a third, container-like ColumnDataSource. Make sure that the columns of all ColumnDataSources share the same name.

    Here is an example, based on your code:

        plot_2s = ColumnDataSource(data=dict(x=[1, 2, 3], y=[1, 2, 3]))
        plot_3s = ColumnDataSource(data=dict(x=[3, 4, 5], y=[1, 2, 3]))
        line_source = ColumnDataSource(data=dict(x=[1, 2, 3], y=[1, 2, 3]))
    
        plot_1 = figure(x_axis_type="datetime", plot_width=800, tools=bokeh_tools, title="plot_1")
        plot_1.line(x_values, y_values)
        plot_2 = figure(x_axis_type="datetime", plot_width=800, tools=bokeh_tools, title="plot_2")
        plot_2.line(x='x', y='y', source=line_source)
    
        select = Select(title="SELECT", options=["val1", "val2"])
        column = layouts.Column(plot_1, select, plot_2)
    
        select.callback = CustomJS(args={"cds2": plot_2s, "cds3": plot_3s, "ls": line_source}, code="""
    
             if(cb_obj.value === "val1"){ 
                     ls.data = cds2.data;
             }else if(cb_obj.value === "val2"){
                     ls.data = cds3.data;
             }
             ls.change.emit();
             """)