1

I've got a 4D dataset and hvplot is nicely generating widgets for the dimensions I've specified in the groupby:

import xarray as xr

ds = xr.open_dataset('http://gamone.whoi.edu/thredds/dodsC/coawst_4/use/fmrc/coawst_4_use_best.ncd')

rho_vars = ['salt','temp']

from cartopy import crs as ccrs
import hvplot.xarray
import holoviews as hv
from geoviews import tile_sources as gvts
import panel as pn

var_select = pn.widgets.Select(name='Variables:', options=rho_vars, 
                               value='temp')

crs = ccrs.PlateCarree()

def plot(var=None):
    base_map = gvts.OSM
    var = var or var_select.value
    mesh = ds[var][-24:,:,:].hvplot.quadmesh(x='lon_rho', y='lat_rho', rasterize=True, title=var,
                                    width=600, height=400, crs=crs,
                                    groupby=list(ds[var].dims[:-2]), cmap='jet')
    overlay = (base_map * mesh.opts(alpha=0.7)).opts(active_tools=['wheel_zoom', 'pan'])
    widgets = {dim: pn.widgets.Select for dim in ds[var].dims[:-2]}
    return pn.holoviews.HoloViews(overlay, widgets=widgets).layout

def on_var_select(event):
    var = event.obj.value
    dashboard[-1] = plot(var=var)

var_select.param.watch(on_var_select, parameter_names=['value']);

dashboard = pn.Column(var_select, plot(var_select.value))

which produces:

enter image description here

However, while hvplot defaults to the first value of the options OrderedDict, I would like to default to the last value.

If I look at the dashboard elements:

print(dashboard)

Column
    [0] Select(name='Variables:', options=OrderedDict([('salt', ...]), value='temp')
    [1] Row
        [0] HoloViews(DynamicMap, widgets={'time': <class 'panel.wid...})
        [1] Column
            [0] Select(name='Forecast time f..., options=OrderedDict([('2019-03-09 ...]), value=numpy.datetime64('2019-03-...)
            [1] Select(name='S-coordinate a..., options=OrderedDict([('-0.96875', ...]), value=-0.96875)

I see that I can specify the Select widget value post facto like:

dashboard[1][1][1].value = next(reversed(dashboard[1][1][1].options.values())) 

and that indeed works:

enter image description here

I would like to make a stand alone dashboard, however, that defaults to a last value when the user selects a new variable. With the current code, when a new variable is selected, the default value is unfortunately again set to the first value.

Is there a way to embed my post facto code to select the last value into my plot function, or is there another better approach to accomplish this?

Full notebook here.

Rich Signell
  • 14,842
  • 4
  • 49
  • 77

2 Answers2

1

If I understand your question correctly, the 'fields' argument to hvplot can do this.

0

As you can see when you print(dashboard) you have a default value of widgets.



Column
    [0] Select(name='Variables:', options=OrderedDict([('salt', ...]), value='temp')
    [1] Row
        [0] HoloViews(DynamicMap, widgets={'time': <class 'panel.wid...})
        [1] Column
            [0] Select(name='Forecast time f..., options=OrderedDict([('2019-03-09 ...]), value=numpy.datetime64('2019-03-...)
            [1] Select(name='S-coordinate a..., options=OrderedDict([('-0.96875', ...]), value=-0.96875)

Here, the default value is -0.96875 for the second widget.

I would work on this line widgets = {dim: pn.widgets.Select for dim in ds[var].dims[:-2]} to put default value.

default_value = [...,...]
widgets = {dim: pn.widgets.Select(value=default_val[dim]) for dim in ds[var].dims[:-2]}
Thomas PEDOT
  • 943
  • 1
  • 9
  • 18
  • Thomas, this seems like the right track, but when I specify `Select(value=` all the other parameters like `Select(name=, options=` are lost. It's frustrating! – Rich Signell Mar 12 '19 at 21:05
  • Worth opening an issue for this in panel, we should consider finding a nice way to specify this. I'm not sure if modifying widgets a user constructed would be the best approach even it would be convenient in this case. – philippjfr Mar 13 '19 at 03:06