-
Notifications
You must be signed in to change notification settings - Fork 0
/
datepicker.py
127 lines (100 loc) · 3.11 KB
/
datepicker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import streamlit as st
import datetime
# Dict of label -> [days ago 0, days ago 1]
# Or None for Custom.
RELATIVE_DATE_RANGES = {
"Custom": None,
"Last 7 days": (7, 0),
"Last 14 days": (14, 0),
"Last 30 days": (30, 0),
"Last 90 days": (90, 0),
"Last year": (365, 0),
}
def relative_date_range_picker(
label,
relative_dates=RELATIVE_DATE_RANGES,
value="Last 7 days",
key="date-range-picker",
):
today = datetime.date.today()
relative_date_key = f"{key}-relative"
is_using_custom_date = getattr(st.session_state, relative_date_key, None) == "Custom"
if is_using_custom_date:
rel_col, abs_col = st.columns(2)
else:
rel_col = st.columns(1)[0] # Using this instead of "st" to catch column-chaining warnings early.
abs_col = None
relative_dates_list = list(relative_dates.keys())
index = relative_dates_list.index(value)
relative_date_str = rel_col.selectbox(
label,
relative_dates_list,
index=index,
key=relative_date_key,
)
if is_using_custom_date:
default_range = relative_dates[value]
if default_range is None:
default_range = [7, 0]
date_range = abs_col.date_input(
"Custom date",
[
today - datetime.timedelta(days=default_range[0]),
today - datetime.timedelta(days=default_range[1]),
],
label_visibility="hidden",
)
else:
relative_date_range = relative_dates[relative_date_str]
date_range = (
today - datetime.timedelta(days=relative_date_range[0]),
today - datetime.timedelta(days=relative_date_range[1]),
)
return date_range
# Dict of label -> days ago
# Or None for Custom
RELATIVE_DATES = {
"Custom": None,
"Today": 0,
"Yesterday": 1,
"7 days ago": 7,
"14 days ago": 14,
"30 days ago": 30,
"90 days ago": 90,
"1 year ago": 365,
}
def relative_date_picker(
label,
relative_dates=RELATIVE_DATES,
value="Today",
key="date-picker",
):
today = datetime.date.today()
relative_date_key = f"{key}-relative"
is_using_custom_date = getattr(st.session_state, relative_date_key, None) == "Custom"
if is_using_custom_date:
rel_col, abs_col = st.columns(2)
else:
rel_col = st.columns(1)[0] # Using this instead of "st" to catch column-chaining warnings early.
abs_col = None
relative_dates_list = list(relative_dates.keys())
index = relative_dates_list.index(value)
relative_date_str = rel_col.selectbox(
label,
relative_dates.keys(),
index=index,
key=relative_date_key,
)
if is_using_custom_date:
default_date = relative_dates[value]
if default_date is None:
default_date = 0
abs_date = abs_col.date_input(
"Custom date",
today - datetime.timedelta(days=default_date),
label_visibility="hidden",
)
else:
relative_date = relative_dates[relative_date_str]
abs_date = today - datetime.timedelta(days=relative_date)
return abs_date