Suppose you have a File Selector which you bind to a function so that when the user selects a file it is loaded as the Xarray dataset used for the rest of the app. Once the file is loaded I want to create widgets dynamically based on the data variable chosen by the user. That is two levels of dynamic behaviour and I am not sure how to implement it. Here is my attampt using the Reactive API
select_file = pnw.Select(name="File", options=file_list)
select_field = pnw.Select(name="Field", options=list(xds.data_vars))
cmap_sel = pnw.Select(name="Colormap", options=['cool','hot','jet','viridis','brg','rainbow'])
rasterize_toggle = pnw.Toggle(name='Rasterize', button_type='success')
@pn.depends(select_file,select_field)
def load_file(select_file,select_field):
file_path = r"C:\Users\spart\Documents\Anaconda-Work-Dir\{}".format(select_file)
xds = fstd2nc.Buffer(file_path).to_xarray()
sel_dict = {}
for (i,d) in enumerate(xds[select_field].dims):
for (j,c) in enumerate(xds[select_field].coords):
if d == c and (d != 'lon' and d != 'lat'):
if xds[select_field].coords[c].dtype == np.dtype('<M8[ns]'):
time_list = []
for i, el in np.ndenumerate(xds[select_field].coords['time'].values):
time_list.append(np.datetime_as_string(el, timezone='UTC'))
time_slider = pnw.DiscreteSlider(options=time_list)
sel_dict[d] = time_slider
else:
sel_dict[d] = pnw.DiscreteSlider(options=xds[select_field].coords[c].values.tolist())
output = xds[select_field].interactive.sel(**sel_dict).hvplot(kind='quadmesh', rasterize=True, data_aspect=1, frame_height=800,cmap='jet', crs=ccrs.PlateCarree(), projection=ccrs.PlateCarree(), project=True, geo=True, coastline=True, global_extent=True)
return output
The for loop and dynamic widget creation for an interactive Xarray Dataset work if I run them individually but I just need to run them on File select and on Data Variable select while retaining the interactivity.