danishjameel003 commited on
Commit
9d4b171
·
verified ·
1 Parent(s): b9e16dc

Update app.py

Browse files

Add the progress feature

Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -8,9 +8,10 @@ load_dotenv()
8
  data_folder = "data"
9
  preview_folder = "Preview"
10
 
11
- # Extracting text from .txt files
12
  def get_text_files_content(folder):
13
  text = ""
 
 
14
  for filename in os.listdir(folder):
15
  if filename.endswith('.txt'):
16
  with open(os.path.join(folder, filename), 'r', encoding='utf-8') as file:
@@ -29,18 +30,36 @@ def notes_page():
29
  "Trump vs BRICS", "US-China Trade War", "War on Humanity", "Women's Suppression in Afghanistan"
30
  ]
31
 
32
- subject_folders = {subject: os.path.join(data_folder, subject.replace(' ', '_')) for subject in subjects}
33
- preview_folders = {subject: os.path.join(preview_folder, subject.replace(' ', '_')) for subject in subjects}
34
-
 
 
 
 
 
 
 
 
35
  if "completed_subjects" not in st.session_state:
36
  st.session_state.completed_subjects = []
37
-
38
  if "current_subject_index" not in st.session_state:
39
  st.session_state.current_subject_index = 0
40
-
41
  if "grading_enabled" not in st.session_state:
42
  st.session_state.grading_enabled = False
43
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  # Separate completed and unread subjects
45
  unread_subjects = [sub for sub in subjects if sub not in st.session_state.completed_subjects]
46
  completed_subjects = st.session_state.completed_subjects
@@ -55,7 +74,7 @@ def notes_page():
55
  else:
56
  displayed_subjects = subjects
57
 
58
- # Dropdown to directly jump to specific unread note
59
  if filter_option == "Unread" and unread_subjects:
60
  selected_unread_subject = st.sidebar.selectbox("Go to Unread Note:", unread_subjects)
61
  st.session_state.current_subject_index = subjects.index(selected_unread_subject)
@@ -64,9 +83,9 @@ def notes_page():
64
  st.sidebar.subheader("All Subjects")
65
  for i, subject in enumerate(displayed_subjects):
66
  status = "✓" if subject in st.session_state.completed_subjects else ""
67
- st.sidebar.text(f"{i + 1}. {subject} {status}")
68
 
69
- # Get the current subject
70
  selected_subject = subjects[st.session_state.current_subject_index]
71
  st.sidebar.subheader("Current Subject:")
72
  st.sidebar.text(selected_subject)
@@ -93,6 +112,8 @@ def notes_page():
93
  if st.button(f"Mark '{selected_subject}' as Read"):
94
  st.session_state.completed_subjects.append(selected_subject)
95
  st.success(f"Marked '{selected_subject}' as read!")
 
 
96
 
97
  # Check if all notes are completed
98
  if len(st.session_state.completed_subjects) == len(subjects):
@@ -104,6 +125,9 @@ def notes_page():
104
  if st.session_state.current_subject_index < len(subjects) - 1:
105
  if st.button("Go to Next Note"):
106
  st.session_state.current_subject_index += 1
 
 
 
107
  else:
108
  st.error(f"No data available for {selected_subject}.")
109
 
@@ -115,4 +139,4 @@ def main():
115
  notes_page()
116
 
117
  if __name__ == "__main__":
118
- main()
 
8
  data_folder = "data"
9
  preview_folder = "Preview"
10
 
 
11
  def get_text_files_content(folder):
12
  text = ""
13
+ if not os.path.exists(folder):
14
+ return text
15
  for filename in os.listdir(folder):
16
  if filename.endswith('.txt'):
17
  with open(os.path.join(folder, filename), 'r', encoding='utf-8') as file:
 
30
  "Trump vs BRICS", "US-China Trade War", "War on Humanity", "Women's Suppression in Afghanistan"
31
  ]
32
 
33
+ # Map subject name -> folder path
34
+ subject_folders = {
35
+ subject: os.path.join(data_folder, subject.replace(' ', '_'))
36
+ for subject in subjects
37
+ }
38
+ preview_folders = {
39
+ subject: os.path.join(preview_folder, subject.replace(' ', '_'))
40
+ for subject in subjects
41
+ }
42
+
43
+ # Initialize session state variables
44
  if "completed_subjects" not in st.session_state:
45
  st.session_state.completed_subjects = []
 
46
  if "current_subject_index" not in st.session_state:
47
  st.session_state.current_subject_index = 0
 
48
  if "grading_enabled" not in st.session_state:
49
  st.session_state.grading_enabled = False
50
 
51
+ # Calculate progress
52
+ total_subjects = len(subjects)
53
+ completed_count = len(st.session_state.completed_subjects)
54
+ progress_fraction = completed_count / total_subjects # e.g. 0.5 means 50%
55
+ progress_percentage = progress_fraction * 100
56
+
57
+ # --- DISPLAY PROGRESS IN THE SIDEBAR ---
58
+ st.sidebar.title("Your Progress")
59
+ st.sidebar.progress(progress_fraction)
60
+ st.sidebar.write(f"You have completed {completed_count} out of {total_subjects} notes.")
61
+ st.sidebar.write(f"Overall progress: {progress_percentage:.2f}%")
62
+
63
  # Separate completed and unread subjects
64
  unread_subjects = [sub for sub in subjects if sub not in st.session_state.completed_subjects]
65
  completed_subjects = st.session_state.completed_subjects
 
74
  else:
75
  displayed_subjects = subjects
76
 
77
+ # If unread are filtered, allow direct jump
78
  if filter_option == "Unread" and unread_subjects:
79
  selected_unread_subject = st.sidebar.selectbox("Go to Unread Note:", unread_subjects)
80
  st.session_state.current_subject_index = subjects.index(selected_unread_subject)
 
83
  st.sidebar.subheader("All Subjects")
84
  for i, subject in enumerate(displayed_subjects):
85
  status = "✓" if subject in st.session_state.completed_subjects else ""
86
+ st.sidebar.text(f"{i+1}. {subject} {status}")
87
 
88
+ # Show current subject
89
  selected_subject = subjects[st.session_state.current_subject_index]
90
  st.sidebar.subheader("Current Subject:")
91
  st.sidebar.text(selected_subject)
 
112
  if st.button(f"Mark '{selected_subject}' as Read"):
113
  st.session_state.completed_subjects.append(selected_subject)
114
  st.success(f"Marked '{selected_subject}' as read!")
115
+ # Recalculate progress so it updates immediately
116
+ st.experimental_rerun()
117
 
118
  # Check if all notes are completed
119
  if len(st.session_state.completed_subjects) == len(subjects):
 
125
  if st.session_state.current_subject_index < len(subjects) - 1:
126
  if st.button("Go to Next Note"):
127
  st.session_state.current_subject_index += 1
128
+ st.experimental_rerun()
129
+ else:
130
+ st.error(f"No text data found for {selected_subject}.")
131
  else:
132
  st.error(f"No data available for {selected_subject}.")
133
 
 
139
  notes_page()
140
 
141
  if __name__ == "__main__":
142
+ main()