Site icon FSIBLOG

How to Build a Colorful Calendar Using Python

How to Build a Colorful Calendar Using Python

As a Python enthusiast, I love creating visually appealing and functional tools using different libraries. One of my favorite projects is generating a colorful calendar using the rich library. This powerful library allows formatting tables with colors and styles, making the terminal output look more engaging.

I’ll walk you through how to create a fully functional, interactive, and visually appealing calendar using Python. I’ll also share an enhanced version that includes holiday highlights, user input for year selection, and automatic detection of today’s date.

Breaking Down the Code

Imports Required Modules

To start, we need to import the necessary modules:

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

Function Definition

def colorful_calendar(year):

This function takes a year as input and prints a colorful calendar.

Creating a Console Object

console = Console()

This initializes a console instance to print the calendar in a structured way.

Generating Month Data

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

Looping Through Months

for month in range(12):

Fetching Month Name

month_name = calendar.month_name[month + 1]

Creating a Table

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

Adding Columns for Days of the Week

table.add_column("Mon", justify="center", style="green")
table.add_column("Tue", justify="center", style="green")
table.add_column("Wed", justify="center", style="green")
table.add_column("Thu", justify="center", style="green")
table.add_column("Fri", justify="center", style="green")
table.add_column("Sat", justify="center", style="green")
table.add_column("Sun", justify="center", style="red")

Filling the Table with Days

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

Printing the Table

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

Calling the Function

colorful_calendar(2025)

Enhancements & Added Practical Functionality

To make this calendar more practical, we can:

  1. Highlight today’s date if displaying the current year.
  2. Allow user input for year selection.
  3. Show special event days (holidays, weekends, etc.).
  4. Make it interactive using user choice.

Updated Code with More Functionality

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

def colorful_calendar(year, highlight_today=True, show_holidays=False):
    console = Console()
    months = [calendar.monthcalendar(year, m) for m in range(1, 13)]
    today = datetime.date.today()
    holidays = {1: [1], 12: [25]}

    for month in range(12):
        month_name = calendar.month_name[month + 1]
        table = Table(title=f"[bold cyan]{month_name} {year}[/bold cyan]", show_lines=True)
        table.add_column("Mon", justify="center", style="green")
        table.add_column("Tue", justify="center", style="green")
        table.add_column("Wed", justify="center", style="green")
        table.add_column("Thu", justify="center", style="green")
        table.add_column("Fri", justify="center", style="green")
        table.add_column("Sat", justify="center", style="yellow")
        table.add_column("Sun", justify="center", style="red")
        
        for week in months[month]:
            row = []
            for day in week:
                if day == 0:
                    row.append("")
                elif highlight_today and day == today.day and month + 1 == today.month and year == today.year:
                    row.append(f"[bold reverse yellow]{day}[/bold reverse yellow]")
                elif show_holidays and month + 1 in holidays and day in holidays[month + 1]:
                    row.append(f"[bold red]{day}[/bold red]")
                else:
                    row.append(str(day))
            table.add_row(*row)
        console.print(table)
        console.print("\n")

# User input
year_input = int(input("Enter the year for the calendar: "))
show_today = input("Highlight today’s date? (yes/no): ").strip().lower() == "yes"
show_holiday_input = input("Show holiday highlights? (yes/no): ").strip().lower() == "yes"

colorful_calendar(year_input, highlight_today=show_today, show_holidays=show_holiday_input)

Conclusion

This improved script makes the calendar interactive, practical, and visually appealing! It allows user input, highlights today’s date, and marks holidays.

Exit mobile version