Skip to content

Commit 435f4d2

Browse files
committed
final update date slider
1 parent 2e6bb8d commit 435f4d2

File tree

1 file changed

+2
-38
lines changed

1 file changed

+2
-38
lines changed

notebooks/utils.py

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(self, start_date=datetime(2018, 1, 1), end_date=datetime(2023, 12,
4949
index=(0, 11), # Default to a 11-month interval
5050
orientation="horizontal",
5151
continuous_update=False,
52-
readout=False,
52+
readout=True,
5353
behaviour="drag",
5454
layout={"width": "90%", "height": "100px", "margin": "0 auto 0 auto"},
5555
style={
@@ -61,14 +61,6 @@ def __init__(self, start_date=datetime(2018, 1, 1), end_date=datetime(2023, 12,
6161
pd.to_datetime(start_date) + pd.DateOffset(months=12) - timedelta(days=1),
6262
]
6363

64-
self.selected_range_widget = widgets.HTML(
65-
value=self._get_selected_range_html(),
66-
layout={"width": "100%", "margin": "0 auto 0 auto"},
67-
)
68-
69-
def _get_selected_range_html(self):
70-
return f"<div style='text-align: center; font-size: 14px; position: relative; width: 100%; height: 10px; margin-top: 20px;'>Selected range: {self.selected_range[0].strftime('%d %b %Y')} to {self.selected_range[1].strftime('%d %b %Y')}</div>"
71-
7264
def on_slider_change(self, change):
7365
start, end = change["new"]
7466
# keep the interval fixed
@@ -77,7 +69,6 @@ def on_slider_change(self, change):
7769
end = start + pd.DateOffset(months=11)
7870
self.interval_slider.value = (start, end)
7971
self.selected_range = (start, end + pd.DateOffset(months=1) - timedelta(days=1))
80-
self.selected_range_widget.value = self._get_selected_range_html()
8172

8273
def show_slider(self):
8374
self.interval_slider.observe(self.on_slider_change, names="value")
@@ -93,40 +84,13 @@ def show_slider(self):
9384
"""
9485
)
9586

96-
# Generate a list of dates for the ticks every 3 months
97-
tick_dates = pd.date_range(
98-
self.start_date, self.end_date + pd.DateOffset(months=3), freq="4ME"
99-
)
100-
101-
# Create a list of tick labels in the format "Aug 2023"
102-
tick_labels = [date.strftime("%b<br>%Y") for date in tick_dates]
103-
104-
# Calculate the positions of the ticks to align them with the slider
105-
total_days = (self.end_date - self.start_date).days
106-
tick_positions = [
107-
((date - self.start_date).days / total_days * 100) for date in tick_dates
108-
]
109-
110-
# Create a text widget to display the tick labels with calculated positions
111-
tick_widget_html = "<div style='text-align: center; font-size: 12px; position: relative; height: 20px; margin-top: -40px; margin-left: 50px; margin-right: 80px'>"
112-
for label, position in zip(tick_labels, tick_positions):
113-
tick_widget_html += f"<div style='position: absolute; left: {position}%; transform: translateX(-50%);'>{label}</div>"
114-
tick_widget_html += "</div>"
115-
116-
tick_widget = widgets.HTML(
117-
value=tick_widget_html,
118-
layout={"width": "100%", "margin": "0 auto 100 auto"},
119-
)
120-
12187
# Arrange the text widget, interval slider, and tick widget using VBox
12288
vbox_with_ticks = widgets.VBox(
12389
[
12490
descr_widget,
12591
self.interval_slider,
126-
tick_widget,
127-
self.selected_range_widget,
12892
],
129-
layout={"height": "220px"},
93+
layout={"height": "150px"},
13094
)
13195

13296
display(vbox_with_ticks)

0 commit comments

Comments
 (0)