'add a constraint to ipywidgets.datepicker, only allow value between two dates
I'm using the DatePicker widget from ipywidgets. I was wondering if I could set some constraints for the day the user picks. Would it be possible to limit the user to only pick a day between datetime.date(2020, 2, 10) and datetime.date(2020, 2, 16)?
Currently my code looks like this:
dp = widgets.DatePicker(
description='Pick a Date',
disabled=False
)
Many thanks in advance
Solution 1:[1]
I had the same requirement. Didn't find anything in the official documentation, but I have found a hack using javascript to set the min and max tags on the generated html input box that works:
%matplotlib widget
import ipywidgets as widgets
from IPython.display import display, Javascript
date_picker = widgets.DatePicker(
description='Pick a Date',
disabled=False,
)
date_picker.add_class("start-date")
script = Javascript("\
const query = '.start-date > input:first-of-type'; \
document.querySelector(query).setAttribute('min', '2020-12-01'); \
document.querySelector(query).setAttribute('max', '2021-01-01'); \
")
display(date_picker)
display(script)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Statue26 |
