diff --git a/habit_tracker/README.md b/habit_tracker/README.md new file mode 100644 index 000000000..e03722c91 --- /dev/null +++ b/habit_tracker/README.md @@ -0,0 +1,48 @@ +Habit Tracker + +1. Installing Dependencies +To install the required dependencies, run the following command: + +$ pip install -r requirements.txt + +2. Using the Habit Tracker +Run the application using: + +python habit_tracker.py + +Once the GUI opens, you can: + +Add a Habit: Enter the name of a habit in the input field and click "Add Habit." +Mark Habit Done: Enter the habit name and click "Mark Habit Done" when you've completed the habit for the day. +View Habits: Click "View Habits" to display your current habits and their streaks. + +3. Streak and Progress Tracking +The Habit Tracker records each habit's daily streak and saves progress in a habits.json file. Each day you mark a habit as completed, the streak increases by one. Use the "View Habits" option to see how long you've maintained each habit. + +Bonus1 (Fixed Schedule for Habit Completion) +You can set up a fixed daily schedule to automate marking your habits as done. +Create a new Python file in the same directory as habit_tracker.py and code the following: + +from habit_tracker import mark_habit_done +import time +import datetime + +# Define your schedule +times = [[1000, 1100], [1200, 1300], [1400, 1500]] # Time in 24-hour format +habits = ["Habit1", "Habit2", "Habit3"] + +while True: + current_time = int(datetime.datetime.now().strftime("%H%M")) + for i, time_slot in enumerate(times): + if time_slot[0] <= current_time < time_slot[1]: + mark_habit_done(habits[i]) + time.sleep(60) # Check every minute + +* Replace "Habit1", "Habit2", and "Habit3" with your actual habit names, and adjust the times accordingly. + +Bonus2 (Habit Notification System) +You can set up notifications for your habits by installing the plyer library to send desktop notifications: + +pip install plyer + +Use the notification functionality within the app to remind you to mark your habits as done. \ No newline at end of file diff --git a/habit_tracker/habit_tracker.py b/habit_tracker/habit_tracker.py new file mode 100644 index 000000000..c14e2751d --- /dev/null +++ b/habit_tracker/habit_tracker.py @@ -0,0 +1,102 @@ +import tkinter as tk +from tkinter import messagebox +import json +from datetime import datetime + + +# Functions for habit tracking logic +def load_habits(): + try: + with open('habits.json', 'r') as file: + return json.load(file) + except FileNotFoundError: + return {} + + +def save_habits(habits): + with open('habits.json', 'w') as file: + json.dump(habits, file, indent=4) + + +def add_habit(habit_name): + habits = load_habits() + if habit_name not in habits: + habits[habit_name] = {'streak': 0, 'last_done': None} + save_habits(habits) + + +def mark_habit_done(habit_name): + habits = load_habits() + today = datetime.now().strftime('%Y-%m-%d') + if habits.get(habit_name) and habits[habit_name]['last_done'] != today: + habits[habit_name]['streak'] += 1 + habits[habit_name]['last_done'] = today + save_habits(habits) + return True + return False + + +def view_habits(): + habits = load_habits() + return habits + + +# Tkinter UI Functions +def add_habit_ui(): + habit_name = habit_entry.get() + if habit_name: + add_habit(habit_name) + messagebox.showinfo("Success", f"Habit '{habit_name}" + f"' added successfully!") + else: + messagebox.showerror("Error", "Please enter a habit name") + + +def mark_done_ui(): + habit_name = habit_entry.get() + if habit_name: + if mark_habit_done(habit_name): + messagebox.showinfo("Success", f"Habit '{habit_name}" + f"' marked as done for today!") + else: + messagebox.showerror("Error", f"Unable to mark '"f"{habit_name}" + f"' as done. Already done today?") + else: + messagebox.showerror("Error", "Please enter a habit name") + + +def view_habits_ui(): + habits = view_habits() + habit_list.delete(0, tk.END) + for habit, info in habits.items(): + habit_list.insert(tk.END, f"{habit}: {info['streak']} " + f"day streak") + + +# Tkinter UI Setup +root = tk.Tk() +root.title("Habit Tracker") + +# Input for habit name +habit_label = tk.Label(root, text="Habit Name:") +habit_label.pack() + +habit_entry = tk.Entry(root) +habit_entry.pack() + +# Buttons for adding, marking, and viewing habits +add_button = tk.Button(root, text="Add Habit", command=add_habit_ui) +add_button.pack() + +done_button = tk.Button(root, text="Mark Habit Done", command=mark_done_ui) +done_button.pack() + +view_button = tk.Button(root, text="View Habits", command=view_habits_ui) +view_button.pack() + +# Listbox for displaying habits +habit_list = tk.Listbox(root, width=50) +habit_list.pack() + +# Run the Tkinter event loop +root.mainloop() diff --git a/habit_tracker/habits.json b/habit_tracker/habits.json new file mode 100644 index 000000000..f4f4cbc59 --- /dev/null +++ b/habit_tracker/habits.json @@ -0,0 +1,10 @@ +{ + "soccer": { + "streak": 1, + "last_done": "2024-09-29" + }, + "person": { + "streak": 1, + "last_done": "2024-09-29" + } +} \ No newline at end of file diff --git a/habit_tracker/requirements.txt b/habit_tracker/requirements.txt new file mode 100644 index 000000000..8f729d14f --- /dev/null +++ b/habit_tracker/requirements.txt @@ -0,0 +1,2 @@ +plyer >= 2.1.0 +schedule >= 1.1.0