Creating a customizable daily planner in Python can be a fun and practical project. Below is a complete example of a console-based daily planner that lets users add, view, edit, and delete tasks for each day. It supports customization of task categories, priority levels, and saving/loading data from a JSON file for persistence.
pythonimport json
from datetime import datetime
class Task:
def __init__(self, description, category="General", priority="Medium", completed=False):
self.description = description
self.category = category
self.priority = priority
self.completed = completed
def to_dict(self):
return {
"description": self.description,
"category": self.category,
"priority": self.priority,
"completed": self.completed
}
@staticmethod
def from_dict(data):
return Task(
description=data["description"],
category=data.get("category", "General"),
priority=data.get("priority", "Medium"),
completed=data.get("completed", False)
)
class DailyPlanner:
def __init__(self, filename="planner_data.json"):
self.tasks = {} # {date_str: [Task, Task, ...]}
self.filename = filename
self.load()
def add_task(self, date, task):
if date not in self.tasks:
self.tasks[date] = []
self.tasks[date].append(task)
print(f"Task added for {date}.")
def view_tasks(self, date):
if date not in self.tasks or not self.tasks[date]:
print(f"No tasks scheduled for {date}.")
return
print(f"Tasks for {date}:")
for idx, task in enumerate(self.tasks[date], start=1):
status = "✓" if task.completed else "✗"
print(f"{idx}. [{status}] {task.description} | Category: {task.category} | Priority: {task.priority}")
def edit_task(self, date, task_index):
if date not in self.tasks or task_index < 1 or task_index > len(self.tasks[date]):
print("Invalid task number.")
return
task = self.tasks[date][task_index - 1]
print("Editing task:")
print(f"Current description: {task.description}")
new_desc = input("Enter new description (or press Enter to keep): ")
if new_desc.strip():
task.description = new_desc.strip()
print(f"Current category: {task.category}")
new_cat = input("Enter new category (or press Enter to keep): ")
if new_cat.strip():
task.category = new_cat.strip()
print(f"Current priority: {task.priority}")
new_prio = input("Enter new priority (Low/Medium/High) (or press Enter to keep): ")
if new_prio.strip() and new_prio.strip().capitalize() in ["Low", "Medium", "High"]:
task.priority = new_prio.strip().capitalize()
print(f"Current status: {'Completed' if task.completed else 'Not Completed'}")
new_comp = input("Mark as completed? (y/n) (or press Enter to keep): ").lower()
if new_comp == "y":
task.completed = True
elif new_comp == "n":
task.completed = False
print("Task updated.")
def delete_task(self, date, task_index):
if date not in self.tasks or task_index < 1 or task_index > len(self.tasks[date]):
print("Invalid task number.")
return
removed_task = self.tasks[date].pop(task_index - 1)
print(f"Removed task: {removed_task.description}")
def save(self):
data = {date: [task.to_dict() for task in tasks] for date, tasks in self.tasks.items()}
with open(self.filename, "w") as f:
json.dump(data, f, indent=4)
print("Planner saved.")
def load(self):
try:
with open(self.filename, "r") as f:
data = json.load(f)
self.tasks = {date: [Task.from_dict(task) for task in tasks] for date, tasks in data.items()}
print("Planner loaded.")
except FileNotFoundError:
self.tasks = {}
def main():
planner = DailyPlanner()
while True:
print("n--- Daily Planner ---")
print("1. Add task")
print("2. View tasks")
print("3. Edit task")
print("4. Delete task")
print("5. Save and Exit")
choice = input("Select an option (1-5): ").strip()
if choice == "1":
date = input("Enter date (YYYY-MM-DD): ").strip()
try:
datetime.strptime(date, "%Y-%m-%d")
except ValueError:
print("Invalid date format.")
continue
description = input("Task description: ").strip()
category = input("Category (default: General): ").strip() or "General"
priority = input("Priority (Low/Medium/High, default: Medium): ").strip().capitalize() or "Medium"
if priority not in ["Low", "Medium", "High"]:
priority = "Medium"
task = Task(description, category, priority)
planner.add_task(date, task)
elif choice == "2":
date = input("Enter date to view tasks (YYYY-MM-DD): ").strip()
planner.view_tasks(date)
elif choice == "3":
date = input("Enter date of task to edit (YYYY-MM-DD): ").strip()
planner.view_tasks(date)
try:
task_num = int(input("Enter task number to edit: ").strip())
except ValueError:
print("Invalid input.")
continue
planner.edit_task(date, task_num)
elif choice == "4":
date = input("Enter date of task to delete (YYYY-MM-DD): ").strip()
planner.view_tasks(date)
try:
task_num = int(input("Enter task number to delete: ").strip())
except ValueError:
print("Invalid input.")
continue
planner.delete_task(date, task_num)
elif choice == "5":
planner.save()
print("Goodbye!")
break
else:
print("Invalid choice. Please enter a number between 1 and 5.")
if __name__ == "__main__":
main()
Features:
-
Add tasks with description, category, and priority.
-
View all tasks for a specific date.
-
Edit existing tasks including their completion status.
-
Delete tasks.
-
Save all tasks to a JSON file (
planner_data.json) and load them automatically on startup. -
Date input is expected in
YYYY-MM-DDformat. -
Basic input validation and user prompts.
You can extend this planner by adding features like reminders, recurring tasks, or a GUI interface if desired.