Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

param.Date no longer uses pn.widgets.DatePicker #1271

Closed
MarcSkovMadsen opened this issue Apr 15, 2020 · 3 comments · Fixed by #1434
Closed

param.Date no longer uses pn.widgets.DatePicker #1271

MarcSkovMadsen opened this issue Apr 15, 2020 · 3 comments · Fixed by #1434

Comments

@MarcSkovMadsen
Copy link
Collaborator

My Pain

I'm trying to develop a parameterized class with a param.Date parameter. When I show the parameter using pn.Param I dont get the DatePicker just a simple input.

See example at https://panel.holoviz.org/user_guide/Param.html

image

Additional Context

If I try to specify the parameter as pn.widgets.DatePicker directly I get a ValueError: Expected a date value, got a datetime.datetime so maybe something has changed with the new DatePicker.

@MarcSkovMadsen MarcSkovMadsen added the TRIAGE Default label for untriaged issues label Apr 15, 2020
@philippjfr philippjfr added type: bug and removed TRIAGE Default label for untriaged issues labels Apr 16, 2020
@CRiddler
Copy link

CRiddler commented Apr 16, 2020

I'm trying to develop a parameterized class with a param.Date parameter. When I show the parameter using pn.Param I dont get the DatePicker just a simple input.

It seems param.Date is flexible in that it can refer to either a dt.date or a dt.datetime however, panel's default widget mappings causes it to be a DatetimeInput widget, which exclusively returns dt.datetime. By feeding param.Date a dt.datetime (such as in the example), then specifying your widget to be a DatePicker it sees your default value as a dt.datetime type and raises an error. You'll need to ensure your default and bounds for the param.Date are dt.date types, then you can specify your widget mapping to a pn.widgets.DatePicker.

class Test(param.Parameterized):
    # This works for default widget mapping, but breaks when I map this parameter to DatePicker
        # Since the default value is a datetime type.
    datetime = param.Date(dt.datetime(2017, 1, 1), bounds=(dt.datetime(2017, 1, 1), dt.datetime(2017, 2, 1)))
    
    # This works for both the default widget mapping, but it will be coerced to a datetime type
        # This also works when I map it to a DatePicker since the values are all `dt.date` types.
    date = param.Date(dt.date(2017, 1, 1), bounds=(dt.date(2017, 1, 1), dt.date(2017, 2, 1)))

app = Test()
layout = pn.panel(app.param, 
         widgets={
#              "datetime": pn.widgets.DatePicker, # Breaks the implementation since the value in app.Test is a dt.datetime, but the widget expects a dt.date
             "date": pn.widgets.DatePicker # This works since the value inside of app.Test is a dt.date and the widget expects a dt.date
         })

print(layout)
layout

Annotation 2020-04-16 111417

Moreover, there was some discussion over at param on how to organize a parameter that specified a datetime vs a date a little while ago, and I believe they settled with param.CalendarDate to be the dedicated datetime.date type. Looking at panel's Param/widgets mappings, it seems there isn't support for param.CalendarDate yet, so a more direct fix will be to support param.CalendarDate and map it to pn.widgets.DatePickershould clear up your first problem.

pn.param.Param._mapping.update(
    {param.CalendarDate: pn.widgets.DatePicker}
)

class Test(param.Parameterized):
    date = param.CalendarDate(dt.date(2017, 1, 1), bounds=(dt.date(2017, 1, 1), dt.date(2017, 2, 1)))

app = Test()
layout = pn.panel(app.param)
print(layout)
layout

Annotation 2020-04-16 111603

If I try to specify the parameter as pn.widgets.DatePicker directly I get a ValueError: Expected a date value, got a datetime.datetime so maybe something has changed with the new DatePicker.

As far as this goes, it seems that the underlying pn.widgets.DatePicker uses param.Date for the default value and bounds under the hood, which may lead to some not straightforward errors. I think these parameters should be changed to param.CalendarDate's which should cause it to at least raise a more informative error.

Sorry for the super long response! I've had my fair share of scratching my head with panel/param and Dates.

@jbednar
Copy link
Member

jbednar commented Apr 16, 2020

Thanks! I think I agree with all of that, i.e. that the real fixes needed are:

  1. Add support for CalendarDate in Panel's Param pane (in panel/param.py), updating the _mapping to add a line like param.CalendarDate: pn.widgets.DatePicker.
  2. Update pn.widgets.DatePicker to use param.CalendarDate instead of param.Date internally

We may also want to:

  1. Add a more explicit param.Datetime that explicitly accepts only a datetime
  2. Deprecate param.Date in favor of making the user choose between param.Datetime or param.Calendardate (or at least heavily document param.Date to explain the issue)

@ceball
Copy link
Contributor

ceball commented Apr 16, 2020

It would be great if possible for someone to address various param datetime woes together: https://github.com/holoviz/param/labels/datetimes.

Skimming those just now, I also don't see any mention of timezone-aware vs. unaware datetimes, timedeltas, etc etc, which it would be great to consider at the same time (even if just to dismiss).

(I suppose panel could also have the extra pain to consider of "browser datetime" vs "python process datetime", in case they are different...but that could just be a friendly note to add to documentation, for people creating applications to think about.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants