The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a Python-powered timeblocker

A Python-powered timeblocker is a tool that allows you to divide your day into blocks of time allocated to specific tasks. Below is a functional command-line version using Python that:

  • Accepts tasks and time slots.

  • Visualizes a daily schedule.

  • Saves and loads schedules from a JSON file.

  • Uses rich for a colorful terminal output.


Requirements

Install rich for styled terminal output:

bash
pip install rich

Python Timeblocker

python
import json from datetime import datetime, timedelta from rich.console import Console from rich.table import Table import os console = Console() SCHEDULE_FILE = "schedule.json" def load_schedule(): if os.path.exists(SCHEDULE_FILE): with open(SCHEDULE_FILE, "r") as f: return json.load(f) return [] def save_schedule(schedule): with open(SCHEDULE_FILE, "w") as f: json.dump(schedule, f, indent=4) def input_time(prompt): while True: time_str = input(prompt) try: return datetime.strptime(time_str, "%H:%M") except ValueError: print("Invalid time format. Use HH:MM (24-hour clock).") def add_block(schedule): task = input("Enter task name: ") start_time = input_time("Start time (HH:MM): ") end_time = input_time("End time (HH:MM): ") if end_time <= start_time: print("End time must be after start time.") return schedule.append({ "task": task, "start": start_time.strftime("%H:%M"), "end": end_time.strftime("%H:%M") }) save_schedule(schedule) console.print("[green]Task added successfully![/green]") def show_schedule(schedule): if not schedule: console.print("[red]No tasks scheduled yet.[/red]") return table = Table(title="Daily Schedule") table.add_column("Task", style="cyan", no_wrap=True) table.add_column("Start", style="green") table.add_column("End", style="green") for entry in sorted(schedule, key=lambda x: x["start"]): table.add_row(entry["task"], entry["start"], entry["end"]) console.print(table) def clear_schedule(): if os.path.exists(SCHEDULE_FILE): os.remove(SCHEDULE_FILE) console.print("[red]Schedule cleared.[/red]") def main(): schedule = load_schedule() while True: console.print("n[bold yellow]Timeblocker Menu[/bold yellow]") console.print("1. Add Time Block") console.print("2. View Schedule") console.print("3. Clear Schedule") console.print("4. Exit") choice = input("Choose an option: ") if choice == "1": add_block(schedule) elif choice == "2": show_schedule(schedule) elif choice == "3": clear_schedule() schedule = [] elif choice == "4": break else: console.print("[red]Invalid option.[/red]") if __name__ == "__main__": main()

Features

  • Persistence: Tasks are saved to a JSON file (schedule.json).

  • Input Validation: Ensures proper time format and logical time sequence.

  • CLI Output: Uses rich for colorful tables and messages.

  • Simple Navigation: Menu-driven interaction in the terminal.


Optional Enhancements

  • Add reminders/notifications using schedule or APScheduler.

  • Export to Google Calendar (with Google API).

  • Add support for recurring tasks.

  • GUI version using Tkinter or PyQt.

Let me know if you want a version with GUI or API integration.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About