-
Notifications
You must be signed in to change notification settings - Fork 949
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
Add datetime and time pickers #2715
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
89ed4e8
Add TimePicker, DatetimePicker
vidartf 10e7683
Fix generator for unions
vidartf 2df57a7
Regen schemas
vidartf d29affc
Add time + datetime pickers to Widget list example
vidartf 6d6a756
Separate naive/aware datetime pickers
vidartf 35503f4
Fix test
vidartf 3549968
Actually fix test
vidartf 22a8ae3
Fixes plus tests from ipydatetime
vidartf d6681a6
Update ipywidgets/widgets/tests/test_widget_datetime.py
vidartf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
|
||
# Copyright (c) Vidar Tonaas Fauske. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
import pytest | ||
|
||
import datetime | ||
import pytz | ||
|
||
from traitlets import TraitError | ||
|
||
from ..trait_types import ( | ||
time_to_json, | ||
time_from_json, | ||
datetime_to_json, | ||
datetime_from_json, | ||
) | ||
|
||
|
||
def test_time_serialize_none(): | ||
assert time_to_json(None, None) == None | ||
|
||
|
||
def test_time_serialize_value(): | ||
t = datetime.time(13, 37, 42, 7000) | ||
assert time_to_json(t, None) == dict( | ||
hours=13, minutes=37, seconds=42, milliseconds=7 | ||
) | ||
|
||
|
||
def test_time_deserialize_none(): | ||
assert time_from_json(None, None) == None | ||
|
||
|
||
def test_time_deserialize_value(): | ||
v = dict(hours=13, minutes=37, seconds=42, milliseconds=7) | ||
assert time_from_json(v, None) == datetime.time(13, 37, 42, 7000) | ||
|
||
|
||
def test_datetime_serialize_none(): | ||
assert datetime_to_json(None, None) == None | ||
|
||
|
||
def test_datetime_serialize_value(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7000, pytz.utc) | ||
assert datetime_to_json(t, None) == dict( | ||
year=2002, | ||
month=1, # Months are 0-based indices in JS | ||
date=20, | ||
hours=13, | ||
minutes=37, | ||
seconds=42, | ||
milliseconds=7, | ||
) | ||
|
||
|
||
def test_datetime_serialize_non_utz(): | ||
# Non-existant timezone, so it wil never be the local one: | ||
tz = pytz.FixedOffset(42) | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7000, tz) | ||
assert datetime_to_json(t, None) == dict( | ||
year=2002, | ||
month=1, # Months are 0-based indices in JS | ||
date=20, | ||
hours=12, | ||
minutes=55, | ||
seconds=42, | ||
milliseconds=7, | ||
) | ||
|
||
|
||
def test_datetime_deserialize_none(): | ||
assert datetime_from_json(None, None) == None | ||
|
||
|
||
def test_datetime_deserialize_value(): | ||
tz = pytz.FixedOffset(42) | ||
v = dict( | ||
year=2002, | ||
month=1, # Months are 0-based indices in JS | ||
date=20, | ||
hours=13, | ||
minutes=37, | ||
seconds=42, | ||
milliseconds=7, | ||
) | ||
assert datetime_from_json(v, None) == datetime.datetime( | ||
2002, 2, 20, 14, 19, 42, 7000, tz | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
|
||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
import pytest | ||
|
||
import datetime | ||
|
||
import pytz | ||
from traitlets import TraitError | ||
|
||
from ..widget_datetime import DatetimePicker | ||
|
||
|
||
def test_time_creation_blank(): | ||
w = DatetimePicker() | ||
assert w.value is None | ||
|
||
|
||
def test_time_creation_value(): | ||
t = datetime.datetime.now(pytz.utc) | ||
w = DatetimePicker(value=t) | ||
assert w.value is t | ||
|
||
|
||
def test_time_validate_value_none(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7, tzinfo=pytz.utc) | ||
t_min = datetime.datetime(1442, 1, 1, tzinfo=pytz.utc) | ||
t_max = datetime.datetime(2056, 1, 1, tzinfo=pytz.utc) | ||
w = DatetimePicker(value=t, min=t_min, max=t_max) | ||
w.value = None | ||
assert w.value is None | ||
|
||
|
||
def test_time_validate_value_vs_min(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7, tzinfo=pytz.utc) | ||
t_min = datetime.datetime(2019, 1, 1, tzinfo=pytz.utc) | ||
t_max = datetime.datetime(2056, 1, 1, tzinfo=pytz.utc) | ||
w = DatetimePicker(min=t_min, max=t_max) | ||
w.value = t | ||
assert w.value.year == 2019 | ||
|
||
|
||
def test_time_validate_value_vs_max(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7, tzinfo=pytz.utc) | ||
t_min = datetime.datetime(1664, 1, 1, tzinfo=pytz.utc) | ||
t_max = datetime.datetime(1994, 1, 1, tzinfo=pytz.utc) | ||
w = DatetimePicker(min=t_min, max=t_max) | ||
w.value = t | ||
assert w.value.year == 1994 | ||
|
||
|
||
def test_time_validate_min_vs_value(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7, tzinfo=pytz.utc) | ||
t_min = datetime.datetime(2019, 1, 1, tzinfo=pytz.utc) | ||
t_max = datetime.datetime(2056, 1, 1, tzinfo=pytz.utc) | ||
w = DatetimePicker(value=t, max=t_max) | ||
w.min = t_min | ||
assert w.value.year == 2019 | ||
|
||
|
||
def test_time_validate_min_vs_max(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7, tzinfo=pytz.utc) | ||
t_min = datetime.datetime(2112, 1, 1, tzinfo=pytz.utc) | ||
t_max = datetime.datetime(2056, 1, 1, tzinfo=pytz.utc) | ||
w = DatetimePicker(value=t, max=t_max) | ||
with pytest.raises(TraitError): | ||
w.min = t_min | ||
|
||
|
||
def test_time_validate_max_vs_value(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7, tzinfo=pytz.utc) | ||
t_min = datetime.datetime(1664, 1, 1, tzinfo=pytz.utc) | ||
t_max = datetime.datetime(1994, 1, 1, tzinfo=pytz.utc) | ||
w = DatetimePicker(value=t, min=t_min) | ||
w.max = t_max | ||
assert w.value.year == 1994 | ||
|
||
|
||
def test_time_validate_max_vs_min(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7, tzinfo=pytz.utc) | ||
t_min = datetime.datetime(1664, 1, 1, tzinfo=pytz.utc) | ||
t_max = datetime.datetime(1337, 1, 1, tzinfo=pytz.utc) | ||
w = DatetimePicker(value=t, min=t_min) | ||
with pytest.raises(TraitError): | ||
w.max = t_max | ||
|
||
|
||
def test_time_validate_naive(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7, tzinfo=pytz.utc) | ||
t_min = datetime.datetime(1442, 1, 1, tzinfo=pytz.utc) | ||
t_max = datetime.datetime(2056, 1, 1, tzinfo=pytz.utc) | ||
|
||
w = DatetimePicker(value=t, min=t_min, max=t_max) | ||
with pytest.raises(TraitError): | ||
w.max = t_max.replace(tzinfo=None) | ||
with pytest.raises(TraitError): | ||
w.min = t_min.replace(tzinfo=None) | ||
with pytest.raises(TraitError): | ||
w.value = t.replace(tzinfo=None) | ||
|
||
|
||
def test_datetime_tzinfo(): | ||
tz = pytz.timezone('Australia/Sydney') | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7, tzinfo=tz) | ||
w = DatetimePicker(value=t) | ||
assert w.value == t | ||
# tzinfo only changes upon input from user | ||
assert w.value.tzinfo == tz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
|
||
# Copyright (c) Vidar Tonaas Fauske. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
import pytest | ||
|
||
import datetime | ||
|
||
import pytz | ||
from traitlets import TraitError | ||
|
||
from ..widget_datetime import NaiveDatetimePicker | ||
|
||
|
||
def test_time_creation_blank(): | ||
w = NaiveDatetimePicker() | ||
assert w.value is None | ||
|
||
|
||
def test_time_creation_value(): | ||
t = datetime.datetime.today() | ||
w = NaiveDatetimePicker(value=t) | ||
assert w.value is t | ||
|
||
|
||
def test_time_validate_value_none(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7) | ||
t_min = datetime.datetime(1442, 1, 1) | ||
t_max = datetime.datetime(2056, 1, 1) | ||
w = NaiveDatetimePicker(value=t, min=t_min, max=t_max) | ||
w.value = None | ||
assert w.value is None | ||
|
||
|
||
def test_time_validate_value_vs_min(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7) | ||
t_min = datetime.datetime(2019, 1, 1) | ||
t_max = datetime.datetime(2056, 1, 1) | ||
w = NaiveDatetimePicker(min=t_min, max=t_max) | ||
w.value = t | ||
assert w.value.year == 2019 | ||
|
||
|
||
def test_time_validate_value_vs_max(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7) | ||
t_min = datetime.datetime(1664, 1, 1) | ||
t_max = datetime.datetime(1994, 1, 1) | ||
w = NaiveDatetimePicker(min=t_min, max=t_max) | ||
w.value = t | ||
assert w.value.year == 1994 | ||
|
||
|
||
def test_time_validate_min_vs_value(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7) | ||
t_min = datetime.datetime(2019, 1, 1) | ||
t_max = datetime.datetime(2056, 1, 1) | ||
w = NaiveDatetimePicker(value=t, max=t_max) | ||
w.min = t_min | ||
assert w.value.year == 2019 | ||
|
||
|
||
def test_time_validate_min_vs_max(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7) | ||
t_min = datetime.datetime(2112, 1, 1) | ||
t_max = datetime.datetime(2056, 1, 1) | ||
w = NaiveDatetimePicker(value=t, max=t_max) | ||
with pytest.raises(TraitError): | ||
w.min = t_min | ||
|
||
|
||
def test_time_validate_max_vs_value(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7) | ||
t_min = datetime.datetime(1664, 1, 1) | ||
t_max = datetime.datetime(1994, 1, 1) | ||
w = NaiveDatetimePicker(value=t, min=t_min) | ||
w.max = t_max | ||
assert w.value.year == 1994 | ||
|
||
|
||
def test_time_validate_max_vs_min(): | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7) | ||
t_min = datetime.datetime(1664, 1, 1) | ||
t_max = datetime.datetime(1337, 1, 1) | ||
w = NaiveDatetimePicker(value=t, min=t_min) | ||
with pytest.raises(TraitError): | ||
w.max = t_max | ||
|
||
|
||
def test_datetime_tzinfo(): | ||
tz = pytz.timezone('Australia/Sydney') | ||
t = datetime.datetime(2002, 2, 20, 13, 37, 42, 7, tzinfo=tz) | ||
with pytest.raises(TraitError): | ||
w = NaiveDatetimePicker(value=t) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
|
||
# Copyright (c) Vidar Tonaas Fauske. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
import pytest | ||
|
||
import datetime | ||
|
||
from traitlets import TraitError | ||
|
||
from ..widget_time import TimePicker | ||
|
||
|
||
def test_time_creation_blank(): | ||
w = TimePicker() | ||
assert w.value is None | ||
|
||
|
||
def test_time_creation_value(): | ||
t = datetime.time() | ||
w = TimePicker(value=t) | ||
assert w.value is t | ||
|
||
|
||
def test_time_validate_value_none(): | ||
t = datetime.time(13, 37, 42, 7) | ||
t_min = datetime.time(2) | ||
t_max = datetime.time(22) | ||
w = TimePicker(value=t, min=t_min, max=t_max) | ||
w.value = None | ||
assert w.value is None | ||
|
||
|
||
def test_time_validate_value_vs_min(): | ||
t = datetime.time(13, 37, 42, 7) | ||
t_min = datetime.time(14) | ||
t_max = datetime.time(22) | ||
w = TimePicker(min=t_min, max=t_max) | ||
w.value = t | ||
assert w.value.hour == 14 | ||
|
||
|
||
def test_time_validate_value_vs_max(): | ||
t = datetime.time(13, 37, 42, 7) | ||
t_min = datetime.time(2) | ||
t_max = datetime.time(12) | ||
w = TimePicker(min=t_min, max=t_max) | ||
w.value = t | ||
assert w.value.hour == 12 | ||
|
||
|
||
def test_time_validate_min_vs_value(): | ||
t = datetime.time(13, 37, 42, 7) | ||
t_min = datetime.time(14) | ||
t_max = datetime.time(22) | ||
w = TimePicker(value=t, max=t_max) | ||
w.min = t_min | ||
assert w.value.hour == 14 | ||
|
||
|
||
def test_time_validate_min_vs_max(): | ||
t = datetime.time(13, 37, 42, 7) | ||
t_min = datetime.time(14) | ||
t_max = datetime.time(12) | ||
w = TimePicker(value=t, max=t_max) | ||
with pytest.raises(TraitError): | ||
w.min = t_min | ||
|
||
|
||
def test_time_validate_max_vs_value(): | ||
t = datetime.time(13, 37, 42, 7) | ||
t_min = datetime.time(2) | ||
t_max = datetime.time(12) | ||
w = TimePicker(value=t, min=t_min) | ||
w.max = t_max | ||
assert w.value.hour == 12 | ||
|
||
|
||
def test_time_validate_max_vs_min(): | ||
t = datetime.time(13, 37, 42, 7) | ||
t_min = datetime.time(2) | ||
t_max = datetime.time(1) | ||
w = TimePicker(value=t, min=t_min) | ||
with pytest.raises(TraitError): | ||
w.max = t_max |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a couple of these headers here and below could be updated to match the existing ones.