How to change select widget to datetime widget for time?

As part of my Pangeo Project work, I’m trying to convert this COAWST Explorer Panel app
to use a datetime picker instead of a selection widget for the time variable (called ocean_time):


I think this the is correct syntax to create the widget:

import pandas as pd
dt = pd.to_datetime(ds.ocean_time)
datetime_widget = pn.widgets.DatetimePicker(value=dt.min(), start=dt.min(), end=dt.max())

but I can’t figure out how to replace the selection widget for the ocean_time time coordinate to use the datetime_widget instead.

I’m hoping the helpful Holoviz community can help! :slight_smile:

The notebook can be run on Binder here:
badge

If you don’t specify any widgets kwarg:

def plot(ds, var, base_map):
    extra_dims = list(ds[var].dims[:-2])
    da = ds[var].cf.isel(T=slice(-time_vals,-1)).unify_chunks()
    if len(da.shape) == 4:
        mesh = da.hvplot.quadmesh(x=da.cf['longitude'].name, y=da.cf['latitude'].name, 
                              rasterize=True, geo=True, title=var, attr_labels=False, 
                              fields={da.cf['Z'].name: {'default': float(da.cf['Z'].values[-1])}},
                              groupby=extra_dims, cmap='turbo', width=600, grid=True,
                              height=600).opts(alpha=0.7, data_aspect=None, 
                              hooks=[set_tools],
                              active_tools=['pan', 'box_zoom'])
    else:
        mesh = da.hvplot.quadmesh(x=da.cf['longitude'].name, y=da.cf['latitude'].name, 
                              rasterize=True, geo=True, title=var, attr_labels=False, 
                              groupby=extra_dims, cmap='turbo', width=600, grid=True,
                              height=600).opts(alpha=0.7, data_aspect=None, 
                              hooks=[set_tools], active_tools=['pan', 'box_zoom'])
    print(extra_dims)
    return pn.panel(mesh * base_map)

You get a date time slider.
image

Theoretically, you should be able to do:
return pn.panel(mesh * base_map, widgets={"ocean_time": pn.widgets. DatetimePicker})

@ahuang11, That would be great if it worked (a lot simpler!) but when I tried this I got:

“ValueError: failed to validate DatetimePicker(id=‘p1116’, …).value: expected either None or a value of type String, got numpy.datetime64(‘2023-09-08T01:00:00.000000000’)”

Here’s the full gist: Jupyter Notebook Viewer

Can you raise an issue on Panel to support numpy datetime? I think it could be potentially supported by checking if hasattr("tolist") without explicit import of numpy like:

import panel as pn
import numpy as np
import datetime
pn.extension()
npdt = np.datetime64('2019-01-01 01:00')
pn.widgets.DatetimePicker(value=npdt.tolist())

Even if you convert the datetime64 values to datetime values and supply them to the widget, it doesn’t work because hvplot doesn’t leave enough space and the calendar is hidden …
:frowning:

See the full code here,
cell [6] in this notebook!