-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a habit tracker program that can store and hold habits when needed by the user
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"soccer": { | ||
"streak": 1, | ||
"last_done": "2024-09-29" | ||
}, | ||
"person": { | ||
"streak": 1, | ||
"last_done": "2024-09-29" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
plyer >= 2.1.0 | ||
schedule >= 1.1.0 |