-
-
Save mr-easy/c0504d560a8d82a13910fea480f55d2e to your computer and use it in GitHub Desktop.
Make an HTML calendar in a Notebook and highlight some days...
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
# intended for use in a Jupyter Notebook or similar. | |
import calendar | |
from calendar import HTMLCalendar | |
from IPython.display import HTML | |
class HighlightedCalendar(HTMLCalendar): | |
def __init__(self, highlight_dates={}, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self._highlight_dates = {} | |
for color,dates in highlight_dates.items(): | |
for date in dates: | |
self._highlight_dates[date] = color | |
def formatday(self, day, weekday): | |
if day in self._month_highlight: | |
return '<td class="%s" bgcolor="%s">%d</td>' % (self.cssclasses[weekday], self._month_highlight[day], day) | |
else: | |
return super().formatday(day, weekday) | |
def formatmonth(self, theyear, themonth, withyear): | |
self._month_highlight = {d.day:self._highlight_dates[d] for d in self._highlight_dates.keys() if (d.year==theyear) and (d.month==themonth)} | |
return super().formatmonth(theyear, themonth, withyear) | |
# Usage | |
highlight_dates = { | |
'lightgreen': [dt.date(2023,1,1),dt.date(2023,2,2),dt.date(2023,3,5)], | |
'lightblue': [dt.date(2023,8,1),dt.date(2023,7,2),dt.date(2023,6,5)] | |
} | |
HTML(HighlightedCalendar(highlight_dates=highlight_dates).formatyear(2023)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment