Last active
July 15, 2024 18:46
-
-
Save elijahbenizzy/c8768692d2d6c1016ea9b0de5d3e6d11 to your computer and use it in GitHub Desktop.
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
import calendar | |
from IPython.display import HTML,display_html | |
from calendar import HTMLCalendar | |
import pandas as pd | |
import datetime | |
class HighlightedCalendar(HTMLCalendar): | |
def __init__(self, highlight=[], normalized_scale=[], *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self._highlight = highlight | |
self._normalized_scale = normalized_scale | |
def formatday(self, day, weekday): | |
if day in self._highlight: | |
index = self._highlight.index(day) | |
if len(self._normalized_scale) > 0: | |
alpha = self._normalized_scale[index]*.3 | |
else: | |
alpha = 0.3 | |
return f'''<td | |
class="{self.cssclasses[weekday]}" | |
style="background-color:rgba(227, 30, 48, {alpha});"> | |
{day} | |
</td>''' | |
else: | |
return super().formatday(day, weekday) | |
def view_date_range(date_range: pd.Series, scale: pd.Series = None): | |
if scale is not None: | |
max_scale = max(scale) | |
min_scale = min(scale) | |
scale = min_scale + (scale-min_scale)/max_scale | |
else: | |
scale = 1 | |
html = "<table><tr>" | |
month_year_combos = {(date.year, date.month) for date in date_range} | |
combined_df = pd.DataFrame(dict(dates=date_range, scale=scale)) | |
for year, month in month_year_combos: | |
filtered_df = combined_df[ | |
(combined_df.dates.dt.year == year) & | |
(combined_df.dates.dt.month == month) | |
] | |
cal = HighlightedCalendar( | |
highlight=list([item.day for item in filtered_df.dates]), | |
normalized_scale=list(filtered_df.scale) | |
) | |
html += f"<td>{cal.formatmonth(year, month)}</td>" | |
html += "</tr></table>" | |
display_html(HTML(html)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment