Hello,
I have a notebook designed to plot data of different subjects and different runs. Each data set, depending on the run, has a different number of data points (in my case each data point is a point in time). I have select widgets that allow me to select the subject and run I wish to plot. Linked to the plot, I have a player so I can look at the data progress over time. However, I am having trouble updating the end value of the player when changing the subject or run.
Bellow is an example of how my data is set up.
- Declare a directory of the data where the keys are the subject names and the values are a list of tuples as such, (run number, number of time points for that run). A list of the subjects is created from the dictionary.
- I then create the widgets that allow me to select the subject and run I want to plot. I was able to create the run selector dependent on the subject selector using a function “update_run()” and param.watch.
- I then created a function “get_num_tp()” that finds the number of time points for a selected set of data, which is dependent on the subject and run. This is done by using the dictionary created in step 1. This function is not necessary but I find it helpful.
- I then create the player were the end value is generated from the “get_num_tp()” function. I use a similar method from step 2 where I create a function “update_player()” that updates the end value of the player and param.watch to watch both the subject and run value. This is where things go wrong. I always get an error “TypeError: update_player() missing 1 required positional argument: ‘event2’” when executing the code.
- This “print_player_value()” function is only there for example purposes. I prints the player value so I can see if the player changes when I change the run or subject.
- Lastly I display the panel with the subject selector, run selector, and player.
CODE:
data_dict={'sub-01':[('run1',100),('run2',100),('run3',60)],'sub-02':[('run1',200),('run2',100),('run3',50)],'sub-03':[('run1',100),('run2',400)]}
subject_list = list(data_dict.keys())
subject_select = pn.widgets.Select(name='Select Subject',options=subject_list,value=subject_list[0])
run_select = pn.widgets.Select(name='Select Run', options=[data_dict[subject_select.value][i][0] for i in range(0,len(data_dict[subject_select.value]))])
def update_run(event):
run_select.options = [data_dict[event.new][i][0] for i in range(0,len(data_dict[event.new]))]
subject_select.param.watch(update_run,'value')
@pn.depends(subject_select.param.value,run_select.param.value)
def get_num_tp(SBJ,RUN):
num_tp = [data_dict[subject_select.value][i][1] for i in range(0,len(data_dict[subject_select.value])) if data_dict[subject_select.value][i][0] == run_select.value][0]
return num_tp
player = pn.widgets.Player(name='Player', start=0, end=get_num_tp(subject_select.value,run_select.value), value=1,loop_policy='loop', width=800, step=1)
def update_player(event1,event2):
player.end = get_num_tp(event1.new,event2.new)
subject_select.param.watch(update_player,'value')
run_select.param.watch(update_player,'value')
@pn.depends(player.param.value)
def print_player_value(value):
value = str(value)
markdown = pn.pane.Markdown(value)
return markdown
pn.Column(pn.Row(subject_select,run_select),player,print_player_value)
Note that I actually have 3 widgets to watch in my data but I only use 2 in this example for simplicity. I figure it isn’t that much different having 2 or 3 dependent widgets.
If you have any advice or can give any help I would greatly appreciate it.