Bokeh - load tabs only when clicked

Viewed 707

I am relatively new to Bokeh and have written a function that allows a user to choose which data to plot using tabs. The function make_plot() below is relatively slow because the dataset being plotted is large, and I have 30 tabs so I would like to only create the plot when a user clicks on a tab (not pre-load all 30 plots). I don't have experience with javascript, is there a way I can do this in Python?

Here is my function:

def plot_all_outputs(sa_dict, min_val=0.01, top=100, stacked=True,
                     error_bars=True, log_axis=True,
                     highlighted_parameters=[]):
    """
    This function calls make_plot() for all the sensitivity
    analysis output files and lets you choose which output to view
    using tabs

    Parameters:
    -----------
    sa_dict                : a dictionary with all the sensitivity analysis
                             results
    min_val                : a float indicating the minimum sensitivity value
                             to be shown
    top                    : integer indicating the number of parameters to
                             display (highest sensitivity values)
    stacked1               : Boolean indicating in bars should be stacked for
                             each parameter.
    error_bars             : Booelan indicating if error bars are shown (True)
                             or are omitted (False)
    log_axis               : Boolean indicating if log axis should be used
                             (True) or if a linear axis should be used (False).
    highlighted_parameters : List of strings indicating which parameter wedges
                             will be highlighted

    Returns:
    --------
    p :  a bokeh plot generated with plotting.make_plot() that includes tabs
         for all the possible outputs.
    """

    tabs_dictionary = {}
    outcomes_array = []

    for files in sa_dict.keys():
        outcomes_array.append(sa_dict[files][0])

    for i in range(len(sa_dict)):
        p = make_plot(outcomes_array[i],
                      top=top,
                      minvalues=min_val,
                      stacked=stacked,
                      errorbar=error_bars,
                      lgaxis=log_axis,
                      highlight=highlighted_parameters
                      )
        tabs_dictionary[i] = Panel(child=p, title=sa_dict.keys()[i])

    tabs = Tabs(tabs=tabs_dictionary.values())
    p = show(tabs)

    return p
1 Answers

In order to plot on tab selection, you could add your code for plotting to the on_change property of your tabs:

tabs = Tabs(tabs=[tab_01,tab_02])
    
def tabs_on_change(attr, old, new):
   print("the active panel is " + str(tabs.active))
   plot_tab_function(tabs.active) #<--your plotting code here
tabs.on_change('active', tabs_on_change)

Here, tabs.active is the index of the selected tab.

Related