I am trying to create a simple example of an app that has interdependent widgets to the tune of this question (whose solution no longer works). Basically I want a pnw.Select
to choose from a pre defined dictionary of folders such as
folders = { 'Name Folder 1': '/path/to/folder/1', 'Name Folder 2': '/path/to/folder/2', 'Name Folder 3': '/path/to/folder/3' }
A pnw.MultiSelect
reacts to the folder change to glob
all the files in that folder and the user can choose the files who are then opened using xarray
and plotted with hvplot
. I have tried @pn.depends(folder_selector.param.value, watch=True)
which crashes my kernel and pn.bind
which throws an error when the hvplot function references the files to be opened. This is a continuation of this question and this question. It is hard to create a minimal reproducible example since it relies on system folders but here is a scaffold code base.
import glob
import collections
import hvplot
import panel as pn
import panel.widgets as pnw
import warnings
pn.extension(sizing_mode = 'stretch_width', template="material")
base_path_options = {
'Name Folder 1': '/path/to/folder/1/*' ,
'Name Folder 2': '/path/to/folder/2/*' ,
'Name Folder 3': '/path/to/folder/3/*' ,
}
def update_file_list_options(event):
file_list = glob.glob(event.value)
files_select.options = file_list
files_select.value = [file_list[0]]
base_path_select = pnw.Select(name='Choose Folder',options=base_path_options)
file_list = glob.glob('/path/to/folder/1/*')
files_select = pnw.MultiChoice(name='Choose Files to Visualize',options=file_list,value=[file_list[0]])
# base_path_select.param.watch(update_file_list_options, 'value')
pn.bind(update_file_list_options, event=base_path_select)
def open_dataset(files) -> xr.Dataset:
xd = fstd2nc.Buffer(files).to_xarray()
return xd
def get_sub_dataset(file, field):
xds = open_dataset(file)
return xds[field]
def plot(xds_sub, colormap):
return xds_sub.hvplot(
kind="quadmesh",
rasterize=True,
data_aspect=1,
frame_height=500,
cmap=colormap,
crs=ccrs.PlateCarree(),
projection=ccrs.PlateCarree(),
project=True,
geo=True,
coastline=True,
global_extent=True,
widget_location='bottom'
)
def main_component():
xds = open_dataset(files_select.value)
select_field = pnw.Select(name="Field", value=list(xds.data_vars)[0],options=list(xds.data_vars))
cmap_sel = pnw.Select(
name="Colormap",
value="jet",
options=["cool", "hot", "jet", "viridis", "brg", "rainbow"]
)
return [
base_path_select,
files_select,
select_field,
cmap_sel,
pn.bind(sub_component, file=files_select, field=select_field, colormap=cmap_sel)
]
def sub_component(file, field, colormap):
xds_sub = get_sub_dataset(file=file, field=field)
return pn.Column(
pn.panel(pn.bind(plot, xds_sub, colormap)),
)