Skip to content

Commit 01aedd9

Browse files
authored
Habit tracker folder
Added a habit tracker program that can store and hold habits when needed by the user
1 parent 0fa47cd commit 01aedd9

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

habit_tracker/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Habit Tracker
2+
3+
1. Installing Dependencies
4+
To install the required dependencies, run the following command:
5+
6+
$ pip install -r requirements.txt
7+
8+
2. Using the Habit Tracker
9+
Run the application using:
10+
11+
python habit_tracker.py
12+
13+
Once the GUI opens, you can:
14+
15+
Add a Habit: Enter the name of a habit in the input field and click "Add Habit."
16+
Mark Habit Done: Enter the habit name and click "Mark Habit Done" when you've completed the habit for the day.
17+
View Habits: Click "View Habits" to display your current habits and their streaks.
18+
19+
3. Streak and Progress Tracking
20+
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.
21+
22+
Bonus1 (Fixed Schedule for Habit Completion)
23+
You can set up a fixed daily schedule to automate marking your habits as done.
24+
Create a new Python file in the same directory as habit_tracker.py and code the following:
25+
26+
from habit_tracker import mark_habit_done
27+
import time
28+
import datetime
29+
30+
# Define your schedule
31+
times = [[1000, 1100], [1200, 1300], [1400, 1500]] # Time in 24-hour format
32+
habits = ["Habit1", "Habit2", "Habit3"]
33+
34+
while True:
35+
current_time = int(datetime.datetime.now().strftime("%H%M"))
36+
for i, time_slot in enumerate(times):
37+
if time_slot[0] <= current_time < time_slot[1]:
38+
mark_habit_done(habits[i])
39+
time.sleep(60) # Check every minute
40+
41+
* Replace "Habit1", "Habit2", and "Habit3" with your actual habit names, and adjust the times accordingly.
42+
43+
Bonus2 (Habit Notification System)
44+
You can set up notifications for your habits by installing the plyer library to send desktop notifications:
45+
46+
pip install plyer
47+
48+
Use the notification functionality within the app to remind you to mark your habits as done.

habit_tracker/habit_tracker.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import tkinter as tk
2+
from tkinter import messagebox
3+
import json
4+
from datetime import datetime
5+
6+
7+
# Functions for habit tracking logic
8+
def load_habits():
9+
try:
10+
with open('habits.json', 'r') as file:
11+
return json.load(file)
12+
except FileNotFoundError:
13+
return {}
14+
15+
16+
def save_habits(habits):
17+
with open('habits.json', 'w') as file:
18+
json.dump(habits, file, indent=4)
19+
20+
21+
def add_habit(habit_name):
22+
habits = load_habits()
23+
if habit_name not in habits:
24+
habits[habit_name] = {'streak': 0, 'last_done': None}
25+
save_habits(habits)
26+
27+
28+
def mark_habit_done(habit_name):
29+
habits = load_habits()
30+
today = datetime.now().strftime('%Y-%m-%d')
31+
if habits.get(habit_name) and habits[habit_name]['last_done'] != today:
32+
habits[habit_name]['streak'] += 1
33+
habits[habit_name]['last_done'] = today
34+
save_habits(habits)
35+
return True
36+
return False
37+
38+
39+
def view_habits():
40+
habits = load_habits()
41+
return habits
42+
43+
44+
# Tkinter UI Functions
45+
def add_habit_ui():
46+
habit_name = habit_entry.get()
47+
if habit_name:
48+
add_habit(habit_name)
49+
messagebox.showinfo("Success", f"Habit '{habit_name}"
50+
f"' added successfully!")
51+
else:
52+
messagebox.showerror("Error", "Please enter a habit name")
53+
54+
55+
def mark_done_ui():
56+
habit_name = habit_entry.get()
57+
if habit_name:
58+
if mark_habit_done(habit_name):
59+
messagebox.showinfo("Success", f"Habit '{habit_name}"
60+
f"' marked as done for today!")
61+
else:
62+
messagebox.showerror("Error", f"Unable to mark '"f"{habit_name}"
63+
f"' as done. Already done today?")
64+
else:
65+
messagebox.showerror("Error", "Please enter a habit name")
66+
67+
68+
def view_habits_ui():
69+
habits = view_habits()
70+
habit_list.delete(0, tk.END)
71+
for habit, info in habits.items():
72+
habit_list.insert(tk.END, f"{habit}: {info['streak']} "
73+
f"day streak")
74+
75+
76+
# Tkinter UI Setup
77+
root = tk.Tk()
78+
root.title("Habit Tracker")
79+
80+
# Input for habit name
81+
habit_label = tk.Label(root, text="Habit Name:")
82+
habit_label.pack()
83+
84+
habit_entry = tk.Entry(root)
85+
habit_entry.pack()
86+
87+
# Buttons for adding, marking, and viewing habits
88+
add_button = tk.Button(root, text="Add Habit", command=add_habit_ui)
89+
add_button.pack()
90+
91+
done_button = tk.Button(root, text="Mark Habit Done", command=mark_done_ui)
92+
done_button.pack()
93+
94+
view_button = tk.Button(root, text="View Habits", command=view_habits_ui)
95+
view_button.pack()
96+
97+
# Listbox for displaying habits
98+
habit_list = tk.Listbox(root, width=50)
99+
habit_list.pack()
100+
101+
# Run the Tkinter event loop
102+
root.mainloop()

habit_tracker/habits.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"soccer": {
3+
"streak": 1,
4+
"last_done": "2024-09-29"
5+
},
6+
"person": {
7+
"streak": 1,
8+
"last_done": "2024-09-29"
9+
}
10+
}

habit_tracker/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
plyer >= 2.1.0
2+
schedule >= 1.1.0

0 commit comments

Comments
 (0)