Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions shiny/ui/_input_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def input_date(
value
The starting date. Either a :class:`~datetime.date` object, or a string in
`yyyy-mm-dd` format. If None (the default), will use the current date in the
client's time zone.
client's time zone. If an empty string is passed, the date picker will be blank.
min
The minimum allowed date. Either a :class:`~datetime.date` object, or a string in
yyyy-mm-dd format.
Expand Down Expand Up @@ -310,6 +310,9 @@ def _date_input_tag(
def _as_date_attr(x: Optional[date | str]) -> Optional[str]:
if x is None:
return None
if isinstance(x, date):
return str(x)
return str(date.fromisoformat(x))
if isinstance(x, str):
if len(x) == 0:
return x
x = date.fromisoformat(x)
# Using strftime here to ensure we just just a date, regardless of if H:M:S are included
return x.strftime("%Y-%m-%d")
4 changes: 2 additions & 2 deletions shiny/ui/_input_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,6 @@ def update_date(
An input label.
value
The starting date. Either a `date()` object, or a string in yyyy-mm-dd format.
If ``None`` (the default), will use the current date in the client's time zone.
min
The minimum allowed value.
max
Expand All @@ -475,14 +474,15 @@ def update_date(

Note
----
{note}
You cannot update the value of a date input to `None` or an empty string.

See Also
--------
* :func:`~shiny.ui.input_date`
"""

session = require_active_session(session)

msg = {
"label": session._process_ui(label) if label is not None else None,
"value": _as_date_attr(value),
Expand Down
93 changes: 93 additions & 0 deletions tests/playwright/shiny/components/datepicker/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from datetime import date

from dateutil.relativedelta import relativedelta

from shiny import App, Inputs, Outputs, Session, reactive, render, ui

min_date = date.fromisoformat("2011-11-04")
max_date = min_date + relativedelta(days=10)

# Our input requires strings to be in the format "YYYY-MM-DD"
str_min = min_date.strftime("%Y-%m-%d")
str_max = max_date.strftime("%Y-%m-%d")

app_ui = ui.page_fluid(
ui.input_date(
"start_date_picker",
"Date Type Input:",
value=max_date,
min=min_date,
max=max_date,
format="dd.mm.yyyy",
language="en",
),
ui.output_text("start"),
ui.input_date(
"min_date_picker",
"Date Type Input:",
value=min_date,
min=min_date,
max=max_date,
format="dd.mm.yyyy",
language="en",
),
ui.output_text("min"),
ui.input_date(
"str_date_picker",
"String Type Input:",
value="2023-10-01",
min="2000-01-01",
max="2023-10-01",
format="dd-mm-yyyy",
language="en",
),
ui.output_text("str_format"),
ui.input_date("none_date_picker", "None Type Input:"),
ui.output_text("none_format"),
ui.input_date(
"empty_date_picker",
"empty Type Input:",
value="",
min=None,
max=None,
format="dd-mm-yyyy",
language="en",
),
ui.output_text("empty_format"),
ui.input_action_button("update", "Update Dates"),
)


def server(input: Inputs, output: Outputs, session: Session):
@render.text
def start():
return "Date Picker Value: " + str(input.start_date_picker())

@render.text
def min():
return "Date Picker Value: " + str(input.min_date_picker())

@render.text
def str_format():
return "Date Picker Value: " + str(input.str_date_picker())

@render.text
def none_format():
return "Date Picker Value: " + str(input.none_date_picker())

@render.text
def empty_format():
return "Date Picker Value: " + str(input.empty_date_picker())

@reactive.effect
@reactive.event(input.update, ignore_none=True, ignore_init=True)
def _():
d = date.fromisoformat("2011-11-05")
ui.update_date("start_date_picker", value=d)
ui.update_date("str_date_picker", value="2020-01-01")

# Note: You cannot update the value of a date input to None (it will be dropped).
# Note: You cannot update the value of a date input to an empty string. This is a Bootstrap Datepicker limitation.


app = App(app_ui, server, debug=True)
59 changes: 59 additions & 0 deletions tests/playwright/shiny/components/datepicker/test_datepicker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import datetime

from playwright.sync_api import Page

from shiny.playwright import controller
from shiny.run import ShinyAppProc


def test_dynamic_navs(page: Page, local_app: ShinyAppProc) -> None:
page.goto(local_app.url)

# Check the initial state of the date picker where value = max
date1 = controller.InputDate(page, "start_date_picker")
date1.expect_value("14.11.2011")

# Can't find a way to inspect the actual display date in the datepicker,
# as even in the broken example, the correct attribute was still set.
# To work around this, we will check the displayed value to ensure the
# correct value is being passed to the server.
date_output = controller.OutputText(page, "start")
date_output.expect_value("Date Picker Value: 2011-11-14")

# Test the date picker with the min date = initial value (unchanged case)
date2 = controller.InputDate(page, "min_date_picker")
date2.expect_value("04.11.2011")

date_output2 = controller.OutputText(page, "min")
date_output2.expect_value("Date Picker Value: 2011-11-04")

# Test the date picker with the date set as a string instead of as a date object
date3 = controller.InputDate(page, "str_date_picker")
date3.expect_value("01-10-2023")

date_output3 = controller.OutputText(page, "str_format")
date_output3.expect_value("Date Picker Value: 2023-10-01")

# Test the date picker with a None value inputted
# None defaults to today's date
input = datetime.date.today().strftime("%Y-%m-%d")
date4 = controller.InputDate(page, "none_date_picker")
date4.expect_value(input)

date_output4 = controller.OutputText(page, "none_format")
date_output4.expect_value("Date Picker Value: " + input)

# Test the default value when an empty string is passed is correctly blank
date5 = controller.InputDate(page, "empty_date_picker")
date5.expect_value("")

date_output5 = controller.OutputText(page, "empty_format")
date_output5.expect_value("Date Picker Value: None")

controller.InputActionButton(page, "update").click()

date1.expect_value("05.11.2011")
date_output.expect_value("Date Picker Value: 2011-11-05")

date3.expect_value("01-01-2020")
date_output3.expect_value("Date Picker Value: 2020-01-01")
Loading