How to reduce interact time (reduce execution times)

Hi , all.

Now I am making item selection in FastListTemplate. at this time , I want to reduce interact times. Here is my sample screen shot.

[ Screen Shot ]


[My problem]

I selected one-item(weight) in Graph tab. And this value transfer to List tab and select in table. It works fine. But my code execute many times selection table code.
Here is proof.
20211119_03.png


above photo , table list selection code execute 3times ( 8,9,10 times ) although my intend execute one time.

And my simple code is below

[ My simple code ]

from bokeh.sampledata.autompg import autompg
import pandas as pd;
import panel as pn
import param
from panel.template import DarkTheme,DefaultTheme
pn.extension()

import hvplot.pandas
columns = list(autompg.columns[:-2])
x = pn.widgets.Select(value='mpg', options=columns, name='x')
y = pn.widgets.Select(value='hp', options=columns, name='y')
color = pn.widgets.ColorPicker(name='Color', value='#AA0505')

# ----------------------------------------------------------------------
#   Selcetion Table
# ----------------------------------------------------------------------

class ReactiveTable(param.Parameterized):
    table = param.DataFrame(default=pd.DataFrame(columns))
    v1 = param.Number()    

    def __init__(self, **params):
        super().__init__(**params)
        self.table_widget = pn.Param(self.param.table)[0]
    @param.depends('table_widget.selection')
    def update_params_with_selected_row_values(self):
        param_test = self.table_widget.selection
        if len(param_test) != 0 :
            self.v1 += 1
            print ('20211118 Table list selection: ', param_test,columns[param_test[0]], 'exe ' , self.v1 , 'times')        
            x.value = columns[param_test[0]]
        return self.table_widget.selection  

rt = ReactiveTable()
# ----------------------------------------------------------------------
#   index search
# ----------------------------------------------------------------------
def search_index(lst, value):
    return [i for i, x in enumerate(lst) if x == value]
# ----------------------------------------------------------------------
#   Graph tab
# ----------------------------------------------------------------------
@pn.depends(x, y, color)
def autompg_plot(xval, yval, colorval ):
    rt.table_widget.selection =search_index(columns,xval)
    print ('20211118 Graph selection: ',rt.table_widget.selection)  
    return autompg.hvplot.scatter(xval, yval, c=colorval)

# ----------------------------------------------------------------------
#   Table tab
# ----------------------------------------------------------------------
@pn.depends( x, y, color)
def test_list( x, y, color):
    ret_view = pn.Column(pn.Row(rt.param.v1),rt.table_widget, rt.update_params_with_selected_row_values)
    return ret_view
# ----------------------------------------------------------------------
#   main screen
# ----------------------------------------------------------------------
select_tab = 0
def main_tabs(sel_tab = select_tab):
    ret_main_tabs = pn.Tabs(
        ('Graph', pn.Row(pn.Column('## MPG Explorer', x, y, color), autompg_plot)),
        ('List', test_list)
        )

    ret_main_tabs.active = sel_tab
    return ret_main_tabs
# ----------------------------------------------------------------------
#   make template and run server
# ----------------------------------------------------------------------
template = pn.template.FastListTemplate(
    title='Panel Sample',
    site='Panel',
    main=main_tabs(select_tab),
    theme = DarkTheme ,
    theme_toggle = True , 
)

template.servable();
pn.serve(template)

Above is a simple code. so execution delay is no problem . But my real program is big. so I can’t ignore this execution delay. so someone help me teaching to reduce interact time?

1 Like

I think the main problem is that you’re having a loop when you call
test_list it calls rt.update_params_with_selected_row_values which calls test_list

So if you change the test_list to:

def test_list():
    ret_view = pn.Column(
        pn.Row(rt.param.v1), rt.table_widget, rt.update_params_with_selected_row_values
    )
    return ret_view

You don’t get the extra trigger. Note this means that your code need to be restructured a bit to work as you intend.
image

A small note: You should use either template.servable() with the command panel serve filename.py or pn.serve(template) with python filename.py but not both.

2 Likes

@Hoxbro Thank you for your quick response. But I’m sorry for late response. Yeah, my code has a loop. But I didn’t find the way to avoid it. Thanks to your instruction , My code run as I want. in addition to this instruction and you advice me how to use pn.serve(template) . I delete duplicate command. Thanks well.

My final code

from bokeh.sampledata.autompg import autompg
import pandas as pd;
import panel as pn
import param
from panel.template import DarkTheme,DefaultTheme
pn.extension()
import hvplot.pandas
columns = list(autompg.columns[:-2])
x = pn.widgets.Select(value='mpg', options=columns, name='x')
y = pn.widgets.Select(value='hp', options=columns, name='y')
color = pn.widgets.ColorPicker(name='Color', value='#AA0505')
First_Flag = True
State_value = 0     # State --- 0 (Graph,List)
# ----------------------------------------------------------------------
#   Selcetion Table
# ----------------------------------------------------------------------
class ReactiveTable(param.Parameterized):
    table = param.DataFrame(default=pd.DataFrame(columns))
    v1 = param.Number()     

    def __init__(self, **params):
        super().__init__(**params)
        self.table_widget = pn.Param(self.param.table)[0]
    @param.depends('table_widget.selection')
    def update_params_with_selected_row_values(self , fst = First_Flag ):
        param_test = self.table_widget.selection
        if len(param_test) != 0 :
            if First_Flag == False :
                if  template.main[0].active == 1 :
                    self.v1 += 1
                    print ('20211118 Table list selection: ', param_test,columns[param_test[0]], 'exe ' , self.v1 , 'times')
                    x.value = columns[param_test[0]]
                    change_tab(0)        
        return self.table_widget.selection  
rt = ReactiveTable()
# ----------------------------------------------------------------------
#   index search
# ----------------------------------------------------------------------
def search_index(lst, value):
    return [i for i, x in enumerate(lst) if x == value]
# ----------------------------------------------------------------------
#   Graph tab
# ----------------------------------------------------------------------
@pn.depends(x, y, color )
def autompg_plot(xval, yval, colorval , fst = First_Flag ):
    if First_Flag == False :
        if  template.main[0].active == 0 :
            rt.table_widget.selection =search_index(columns,xval)
            print ('20211118 Graph selection: ',rt.table_widget.selection)  
            change_tab(1)  
    return autompg.hvplot.scatter(xval, yval, c=colorval)
# ----------------------------------------------------------------------
#   Table tab
# ----------------------------------------------------------------------
@pn.depends( )
def test_list():
    ret_view = pn.Column(pn.Row(rt.param.v1),rt.table_widget, rt.update_params_with_selected_row_values)
    return ret_view
# ----------------------------------------------------------------------
#   main screen
# ----------------------------------------------------------------------
select_tab = 0
def main_tabs(sel_tab = select_tab):
    ret_main_tabs = pn.Tabs(
        ('Graph', pn.Row(pn.Column('## MPG Explorer', x, y, color), autompg_plot)),
        ('List', test_list)
        )
    ret_main_tabs.active = sel_tab
    return ret_main_tabs
# ----------------------------------------------------------------------
#   make template and run server
# ----------------------------------------------------------------------
template = pn.template.FastListTemplate(
    title='Panel Sample',
    site='Panel',
    main=main_tabs(select_tab),
    theme = DarkTheme ,
    theme_toggle = True , 
)
# template.servable();
# ----------------------------------------------------------------------
#   index search
# ----------------------------------------------------------------------
def change_tab(tab_number):
    template.main[0].active = tab_number
First_Flag = False
pn.serve(template)
3 Likes