diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d1f4ca5e..4fa3f8b78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Fixed several issues with `page_navbar()` styling. (#1124) * Fixed `Renderer.output_id` to not contain the module namespace prefix, only the output id. (#1130) * Fixed gap-driven spacing between children in fillable `nav_panel()` containers. (#1152) +* Fixed #1138: An empty value in a date or date range input would cause an error; now it is treated as `None`. (#1139) ### Other changes diff --git a/shiny/input_handler.py b/shiny/input_handler.py index 32309ab40..01d86067e 100644 --- a/shiny/input_handler.py +++ b/shiny/input_handler.py @@ -99,13 +99,24 @@ def _(value, name, session): @input_handlers.add("shiny.date") def _( value: str | list[str], name: ResolvedId, session: Session -) -> date | tuple[date, date]: +) -> date | None | tuple[date | None, date | None]: + if isinstance(value, str): + return _safe_strptime_date(value) + else: + return ( + _safe_strptime_date(value[0]), + _safe_strptime_date(value[1]), + ) + + +def _safe_strptime_date(value: str | None) -> date | None: + if value is None: + return None + try: return datetime.strptime(value, "%Y-%m-%d").date() - return ( - datetime.strptime(value[0], "%Y-%m-%d").date(), - datetime.strptime(value[1], "%Y-%m-%d").date(), - ) + except ValueError: + return None @input_handlers.add("shiny.datetime")