Hi , all.
Now I am making with a DataTable and scatter in FastListTemplate. at this time . I want change DataTable value when I push random button. Now , When I push the button , the value changed at Graph tab, But doesn’t change Datatable.
Here is a very simplified version of my code. When I click on the button, I expect graph value and DataTable value changes. But now Graph value changes. so It seems to have worked , however, the Table still show original value ← [This is my problem].
Can you help me find what I’m missing ?
##[My Simple code]
import pandas as pd;
import panel as pn
from bokeh.plotting import ColumnDataSource, figure
import param
from panel.template import DarkTheme,DefaultTheme
import random
pn.extension()
# import hvplot.pandas
source = ColumnDataSource(
data=dict(
v1=list(range(0,10,1)),
v2=list(range(0,20,2)),
v3=list(range(0,30,3)),
v4=list(range(0,40,4)),
)
)
x_sel = pn.widgets.Select(value='v1', options=['v1','v2','v3','v4'], name='x')
y_sel = pn.widgets.Select(value='v2', options=['v1','v2','v3','v4'], name='y')
button_random = pn.widgets.Button(name='random')
First_Flag = True
State_value = 0 # State --- 0 (Graph,List)
# ----------------------------------------------------------------------
# Selcetion Table
# ----------------------------------------------------------------------
@pn.depends()
class ReactiveTable(param.Parameterized):
table = param.DataFrame(pd.DataFrame(source.data).T)
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,x_sel.options[param_test[0]], 'exe ' , self.v1 , 'times')
x_sel.value = x_sel.options[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_sel,y_sel)
def autompg_plot(xval, yval, fst = First_Flag ):
if First_Flag == False :
if template.main[0].active == 0 :
rt.table_widget.selection =search_index(x_sel.options,xval)
print ('20211118 Graph selection: ',rt.table_widget.selection)
# change_tab(1)
p1 = figure(width=350, height=350, name='Scatter',title='Graph1' , title_location = 'below')
p1.scatter(x=x_sel.value , y=y_sel.value, source=source, line_alpha=1, color="red")
return p1
# ----------------------------------------------------------------------
# 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
#---------------------------------------------------------------------------------------------------#
# Treat random button #
#---------------------------------------------------------------------------------------------------#
def click_random(click):
rand_num = random.randint(5,10)
r_v1=[]
r_v2=[]
r_v3=[]
r_v4=[]
for i in range(rand_num) :
r_v1.append(random.randint(1,100))
r_v2.append(random.randint(1,100))
r_v3.append(random.randint(1,100))
r_v4.append(random.randint(1,100))
replace_df = pd.DataFrame({'v1':r_v1,'v2':r_v2,'v3':r_v3,'v4':r_v4})
source.data.update(replace_df)
button_random .on_click(click_random)
# ----------------------------------------------------------------------
# main screen
# ----------------------------------------------------------------------
select_tab = 0
def main_tabs(sel_tab = select_tab):
ret_main_tabs = pn.Tabs(
('Graph', pn.Row(pn.Column('## Table_Update', x_sel, y_sel), autompg_plot)),
('List', test_list),
('Random',button_random),
)
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)
##Screen Shot 1 [Initial condition]
##Screen Shot 2 [After click random button. Graph has changed, But DataTable hasn’t changed]