Ahmad-01 commited on
Commit
dcb85b5
Β·
verified Β·
1 Parent(s): 4e46f8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -110
app.py CHANGED
@@ -1,124 +1,102 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import plotly.express as px
4
- from datetime import datetime, timedelta
5
- import json
6
- import os
 
7
 
8
- # ----------------------------
9
- # App Config
10
- # ----------------------------
11
- st.set_page_config(page_title="Habit & To-Do Tracker", layout="wide")
12
- st.title("πŸ“‹ Habit & To-Do Tracker")
13
 
14
- # ----------------------------
15
- # File Storage
16
- # ----------------------------
17
- DATA_FILE = "data.json"
18
 
19
- def load_data():
20
  if os.path.exists(DATA_FILE):
21
  with open(DATA_FILE, "r") as f:
22
- return json.load(f)
23
- return {"tasks": [], "habits": {}}
24
-
25
- def save_data(data):
26
- with open(DATA_FILE, "w") as f:
27
- json.dump(data, f)
28
 
29
- data = load_data()
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- # ----------------------------
32
- # Sidebar: Add Task
33
- # ----------------------------
34
- st.sidebar.header("βž• Add Task")
35
- task_name = st.sidebar.text_input("Task Name")
36
- task_completed = st.sidebar.checkbox("Completed")
37
- if st.sidebar.button("Add Task") and task_name.strip():
38
- today = datetime.today().strftime("%Y-%m-%d")
39
- data["tasks"].append({
40
- "Task": task_name,
41
- "Completed": task_completed,
42
- "Date": today
43
- })
44
- save_data(data)
45
- st.sidebar.success("Task added!")
46
 
47
- # ----------------------------
48
- # Sidebar: Track Habit
49
- # ----------------------------
50
- st.sidebar.header("πŸ”₯ Track Habit")
51
- habit_name = st.sidebar.text_input("Habit Name")
52
- if st.sidebar.button("Mark Habit Done") and habit_name.strip():
53
- today = datetime.today().strftime("%Y-%m-%d")
54
- if habit_name not in data["habits"]:
55
- data["habits"][habit_name] = []
56
- if today not in data["habits"][habit_name]:
57
- data["habits"][habit_name].append(today)
58
- save_data(data)
59
- st.sidebar.success("Habit marked for today!")
60
  else:
61
- st.sidebar.warning("Already marked today.")
62
 
63
- # ----------------------------
64
- # Display Tasks
65
- # ----------------------------
66
- st.subheader("πŸ“Œ Tasks")
67
- if data["tasks"]:
68
- task_df = pd.DataFrame(data["tasks"])
69
- st.dataframe(task_df, use_container_width=True)
70
- else:
71
- st.info("No tasks yet.")
 
 
 
 
 
 
72
 
73
- # ----------------------------
74
- # Display Habits
75
- # ----------------------------
76
- st.subheader("πŸ”₯ Habits")
77
- habit_data = []
78
- for habit, dates in data["habits"].items():
79
- # Sort dates for streak calculation
80
- dates_sorted = sorted(dates)
81
- streak = 0
82
- # Calculate consecutive days streak ending today
83
- today = datetime.today().date()
84
- for i in range(21):
85
- day = today - timedelta(days=i)
86
- if day.strftime("%Y-%m-%d") in dates_sorted:
87
- streak += 1
88
- else:
89
- break
90
- habit_type = "🌟 Permanent Habit" if streak >= 21 else "⏳ Temporary Habit"
91
- habit_data.append({"Habit": habit, "Days Followed": len(dates), "Streak": streak, "Type": habit_type})
92
-
93
- if habit_data:
94
- habit_df = pd.DataFrame(habit_data)
95
- st.dataframe(habit_df, use_container_width=True)
96
 
97
- # ----------------------------
98
- # Visualizations
99
- # ----------------------------
100
- st.subheader("πŸ“Š Habit Analytics")
101
- st.plotly_chart(px.bar(habit_df, x="Habit", y="Days Followed", color="Type"), use_container_width=True)
102
- st.plotly_chart(px.pie(habit_df, names="Habit", values="Days Followed"), use_container_width=True)
103
- else:
104
- st.info("No habits tracked yet.")
 
 
 
105
 
106
- # ----------------------------
107
- # Daily Report
108
- # ----------------------------
109
- st.subheader("πŸ“‘ Daily Report")
110
- today_str = datetime.today().strftime("%Y-%m-%d")
111
- today_tasks = [t for t in data["tasks"] if t["Date"] == today_str]
112
- report_df = pd.DataFrame([{
113
- "Date": today_str,
114
- "Total Tasks Today": len(today_tasks),
115
- "Completed Tasks Today": len([t for t in today_tasks if t["Completed"]]),
116
- "Total Habits": len(data["habits"])
117
- }])
118
- st.table(report_df)
119
 
120
- # ----------------------------
121
- # Download CSV
122
- # ----------------------------
123
- csv_data = report_df.to_csv(index=False).encode("utf-8")
124
- st.download_button("⬇ Download CSV", csv_data, "daily_report.csv", "text/csv")
 
1
+ def main():
2
+ import streamlit as st
3
+ import pandas as pd
4
+ import plotly.express as px
5
+ from datetime import datetime, timedelta
6
+ import json
7
+ import os
8
 
9
+ st.set_page_config(page_title="Habit & To-Do Tracker", layout="wide")
10
+ st.title("πŸ“‹ Habit & To-Do Tracker")
 
 
 
11
 
12
+ DATA_FILE = "data.json"
 
 
 
13
 
14
+ # Load data
15
  if os.path.exists(DATA_FILE):
16
  with open(DATA_FILE, "r") as f:
17
+ data = json.load(f)
18
+ else:
19
+ data = {"tasks": [], "habits": {}}
 
 
 
20
 
21
+ # Sidebar: Add Task
22
+ st.sidebar.header("βž• Add Task")
23
+ task_name = st.sidebar.text_input("Task Name")
24
+ task_completed = st.sidebar.checkbox("Completed")
25
+ if st.sidebar.button("Add Task") and task_name.strip():
26
+ today = datetime.today().strftime("%Y-%m-%d")
27
+ data["tasks"].append({
28
+ "Task": task_name,
29
+ "Completed": task_completed,
30
+ "Date": today
31
+ })
32
+ with open(DATA_FILE, "w") as f:
33
+ json.dump(data, f)
34
+ st.sidebar.success("Task added!")
35
 
36
+ # Sidebar: Track Habit
37
+ st.sidebar.header("πŸ”₯ Track Habit")
38
+ habit_name = st.sidebar.text_input("Habit Name")
39
+ if st.sidebar.button("Mark Habit Done") and habit_name.strip():
40
+ today = datetime.today().strftime("%Y-%m-%d")
41
+ if habit_name not in data["habits"]:
42
+ data["habits"][habit_name] = []
43
+ if today not in data["habits"][habit_name]:
44
+ data["habits"][habit_name].append(today)
45
+ with open(DATA_FILE, "w") as f:
46
+ json.dump(data, f)
47
+ st.sidebar.success("Habit marked for today!")
48
+ else:
49
+ st.sidebar.warning("Already marked today.")
 
50
 
51
+ # Display Tasks
52
+ st.subheader("πŸ“Œ Tasks")
53
+ if data["tasks"]:
54
+ task_df = pd.DataFrame(data["tasks"])
55
+ st.dataframe(task_df, use_container_width=True)
 
 
 
 
 
 
 
 
56
  else:
57
+ st.info("No tasks yet.")
58
 
59
+ # Display Habits
60
+ st.subheader("πŸ”₯ Habits")
61
+ habit_data = []
62
+ for habit, dates in data["habits"].items():
63
+ dates_sorted = sorted(dates)
64
+ streak = 0
65
+ today = datetime.today().date()
66
+ for i in range(21):
67
+ day = today - timedelta(days=i)
68
+ if day.strftime("%Y-%m-%d") in dates_sorted:
69
+ streak += 1
70
+ else:
71
+ break
72
+ habit_type = "🌟 Permanent Habit" if streak >= 21 else "⏳ Temporary Habit"
73
+ habit_data.append({"Habit": habit, "Days Followed": len(dates), "Streak": streak, "Type": habit_type})
74
 
75
+ if habit_data:
76
+ habit_df = pd.DataFrame(habit_data)
77
+ st.dataframe(habit_df, use_container_width=True)
78
+ st.subheader("πŸ“Š Habit Analytics")
79
+ st.plotly_chart(px.bar(habit_df, x="Habit", y="Days Followed", color="Type"), use_container_width=True)
80
+ st.plotly_chart(px.pie(habit_df, names="Habit", values="Days Followed"), use_container_width=True)
81
+ else:
82
+ st.info("No habits tracked yet.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
+ # Daily Report
85
+ st.subheader("πŸ“‘ Daily Report")
86
+ today_str = datetime.today().strftime("%Y-%m-%d")
87
+ today_tasks = [t for t in data["tasks"] if t["Date"] == today_str]
88
+ report_df = pd.DataFrame([{
89
+ "Date": today_str,
90
+ "Total Tasks Today": len(today_tasks),
91
+ "Completed Tasks Today": len([t for t in today_tasks if t["Completed"]]),
92
+ "Total Habits": len(data["habits"])
93
+ }])
94
+ st.table(report_df)
95
 
96
+ # Download CSV
97
+ csv_data = report_df.to_csv(index=False).encode("utf-8")
98
+ st.download_button("⬇ Download CSV", csv_data, "daily_report.csv", "text/csv")
 
 
 
 
 
 
 
 
 
 
99
 
100
+ # Run the app
101
+ if __name__ == "__main__":
102
+ main()