Site icon FSIBLOG

How I Built a Colorful Calendar App in Python

How I Built a Colorful Calendar App in Python

How I Built a Colorful Calendar App in Python

I’m a Python enthusiast, and today I’m excited to share a little project I recently built a colorful, terminal-based calendar that not only looks great but is also practical, with features like highlighting today’s date, recognizing holidays, and even the option to save the output to a file.

This project is powered by Python’s built-in calendar module and the incredible rich library, which allows me to render beautifully styled tables right inside the terminal.

Let’s dive into the code step by step, and I’ll walk you through everything I’ve done.

Explanation of the Code

Importing Modules

import calendar
from rich.console import Console
from rich.table import Table

Defining the Main Function

def colorful_calendar(year):

This function takes a year as input (like 2025) and generates the calendar for all 12 months in that year.

Creating Console and Calendar Data

console = Console()
months = [calendar.monthcalendar(year, m) for m in range(1, 13)]

Loop Through All Month

for month in range(12):
month_name = calendar.month_name[month + 1]

This loops from January to December. month_name gives me the full name like “January”.

Create and Populate the Table

table = Table(title=f"[bold cyan]{month_name} {year}[/bold cyan]", show_lines=True)

This creates a table with a fancy cyan-colored title.

Then, I add columns for the days:

table.add_column("Mon", justify="center", style="green")
...
table.add_column("Sun", justify="center", style="red")

Weekdays are green. Weekends (Sat & Sun) are red.

For the data, I loop over the weeks:

for week in months[month]:
table.add_row(*[str(day) if day != 0 else "" for day in week])

If a day is 0, it means it’s not part of that month, so I just leave it blank.

Print the Table

console.print(table)
console.print("\n")

Finally, I render the table and add a newline for spacing.

Enhancement Making It Practical

Now, that was the basic version.

So, I added some real-world features:

Enhancements:

Final Code with Feature

import calendar
from datetime import datetime
from rich.console import Console
from rich.table import Table

# Sample holidays (MM-DD format)
HOLIDAYS = {
"01-01": "New Year's Day",
"07-04": "Independence Day",
"12-25": "Christmas Day"
}

def colorful_calendar(year, specific_month=None, save_to_file=False):
console = Console(record=save_to_file)
today = datetime.today()

months = [calendar.monthcalendar(year, m) for m in range(1, 13)]

for month in range(12):
if specific_month and (month + 1) != specific_month:
continue # Skip other months if only one is selected

month_name = calendar.month_name[month + 1]

table = Table(title=f"[bold cyan]{month_name} {year}[/bold cyan]", show_lines=True)

# Add columns for days of the week
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
for i, day in enumerate(days):
style = "red" if day in ["Sat", "Sun"] else "green"
table.add_column(day, justify="center", style=style)

for week in months[month]:
row = []
for i, day in enumerate(week):
date_str = f"{month+1:02d}-{day:02d}"
if day == 0:
row.append("")
elif year == today.year and (month + 1) == today.month and day == today.day:
row.append(f"

How You Can Use It

You can tweak this for:

Final Thought

This project was a fun blend of creativity and utility. It taught me how powerful Python’s built-in tools can be, especially when paired with external libraries like rich. Whether you’re building a command-line productivity tool or just flexing your terminal aesthetics, this colorful calendar is a great start.

Exit mobile version