danishjameel003 commited on
Commit
43fcd5d
·
verified ·
1 Parent(s): c6eca13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -25
app.py CHANGED
@@ -5,16 +5,21 @@ from dotenv import load_dotenv
5
  load_dotenv()
6
 
7
  # Constants
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:
 
18
  text += file.read() + "\n"
19
  return text
20
 
@@ -22,21 +27,24 @@ def notes_page():
22
  st.header("CSS Edge - Notes Viewer :books:")
23
 
24
  subjects = [
25
- "A Trumped World", "Agri Tax in Punjab", "Assad's Fall in Syria", "Elusive National Unity", "Europe and Trump 2.0",
26
- "Going Down with Democracy", "Indonesia's Pancasila Philosophy", "Pakistan in Choppy Waters",
27
- "Pakistan's Semiconductor Ambitions", "Preserving Pakistan's Cultural Heritage", "Tackling Informal Economy",
28
- "Technical Education in Pakistan", "The Case for Solidarity Levies", "The Decline of the Sole Superpower",
29
- "The Power of Big Oil", "Trump 2.0 and Pakistan's Emerging Foreign Policy", "Trump and the World 2.0",
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
 
@@ -48,13 +56,13 @@ def notes_page():
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.")
@@ -64,7 +72,7 @@ def notes_page():
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
66
 
67
- # Dropdown to filter subjects
68
  filter_option = st.sidebar.selectbox("Filter Notes:", ["All", "Unread", "Read"])
69
 
70
  if filter_option == "Unread":
@@ -79,18 +87,18 @@ def notes_page():
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)
81
 
82
- # Sidebar list of subjects
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)
92
 
93
- # Display preview content
94
  preview_folder_path = preview_folders[selected_subject]
95
  if os.path.exists(preview_folder_path):
96
  preview_text = get_text_files_content(preview_folder_path)
@@ -99,7 +107,7 @@ def notes_page():
99
  else:
100
  st.error(f"No preview available for {selected_subject}.")
101
 
102
- # Display full notes
103
  subject_folder_path = subject_folders[selected_subject]
104
  if os.path.exists(subject_folder_path):
105
  raw_text = get_text_files_content(subject_folder_path)
@@ -107,21 +115,21 @@ def notes_page():
107
  st.subheader("Notes")
108
  st.text_area("Notes Content:", raw_text, height=500, disabled=True)
109
 
110
- # Mark as completed button
111
  if selected_subject not in st.session_state.completed_subjects:
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):
120
  st.session_state.grading_enabled = True
121
  st.sidebar.success("All subjects completed. You can now proceed to the Grading Tool.")
122
  st.markdown("[Proceed to Grading Tool](https://thecssedge.com/wp-admin/admin.php?page=feedback-dashboard)")
123
 
124
- # Next subject button
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
 
5
  load_dotenv()
6
 
7
  # Constants
8
+ DATA_FOLDER = "data"
9
+ PREVIEW_FOLDER = "Preview"
10
 
11
  def get_text_files_content(folder):
12
+ """
13
+ Reads all .txt files from a given folder and
14
+ returns their combined text content.
15
+ """
16
  text = ""
17
  if not os.path.exists(folder):
18
  return text
19
  for filename in os.listdir(folder):
20
+ if filename.endswith(".txt"):
21
+ file_path = os.path.join(folder, filename)
22
+ with open(file_path, "r", encoding="utf-8") as file:
23
  text += file.read() + "\n"
24
  return text
25
 
 
27
  st.header("CSS Edge - Notes Viewer :books:")
28
 
29
  subjects = [
30
+ "A Trumped World", "Agri Tax in Punjab", "Assad's Fall in Syria",
31
+ "Elusive National Unity", "Europe and Trump 2.0", "Going Down with Democracy",
32
+ "Indonesia's Pancasila Philosophy", "Pakistan in Choppy Waters",
33
+ "Pakistan's Semiconductor Ambitions", "Preserving Pakistan's Cultural Heritage",
34
+ "Tackling Informal Economy", "Technical Education in Pakistan",
35
+ "The Case for Solidarity Levies", "The Decline of the Sole Superpower",
36
+ "The Power of Big Oil", "Trump 2.0 and Pakistan's Emerging Foreign Policy",
37
+ "Trump and the World 2.0", "Trump vs BRICS", "US-China Trade War",
38
+ "War on Humanity", "Women's Suppression in Afghanistan"
39
  ]
40
 
41
+ # Create dict mapping from subject name -> folder path
42
  subject_folders = {
43
+ subject: os.path.join(DATA_FOLDER, subject.replace(" ", "_"))
44
  for subject in subjects
45
  }
46
  preview_folders = {
47
+ subject: os.path.join(PREVIEW_FOLDER, subject.replace(" ", "_"))
48
  for subject in subjects
49
  }
50
 
 
56
  if "grading_enabled" not in st.session_state:
57
  st.session_state.grading_enabled = False
58
 
59
+ # --- CALCULATE AND DISPLAY PROGRESS ---
60
  total_subjects = len(subjects)
61
  completed_count = len(st.session_state.completed_subjects)
62
  progress_fraction = completed_count / total_subjects # e.g. 0.5 means 50%
63
  progress_percentage = progress_fraction * 100
64
 
65
+ # Show progress in the sidebar
66
  st.sidebar.title("Your Progress")
67
  st.sidebar.progress(progress_fraction)
68
  st.sidebar.write(f"You have completed {completed_count} out of {total_subjects} notes.")
 
72
  unread_subjects = [sub for sub in subjects if sub not in st.session_state.completed_subjects]
73
  completed_subjects = st.session_state.completed_subjects
74
 
75
+ # Dropdown to filter notes
76
  filter_option = st.sidebar.selectbox("Filter Notes:", ["All", "Unread", "Read"])
77
 
78
  if filter_option == "Unread":
 
87
  selected_unread_subject = st.sidebar.selectbox("Go to Unread Note:", unread_subjects)
88
  st.session_state.current_subject_index = subjects.index(selected_unread_subject)
89
 
90
+ # Sidebar list of displayed subjects (with read/unread status)
91
  st.sidebar.subheader("All Subjects")
92
  for i, subject in enumerate(displayed_subjects):
93
  status = "✓" if subject in st.session_state.completed_subjects else ""
94
+ st.sidebar.text(f"{i + 1}. {subject} {status}")
95
 
96
+ # Show current subject in the sidebar
97
  selected_subject = subjects[st.session_state.current_subject_index]
98
  st.sidebar.subheader("Current Subject:")
99
  st.sidebar.text(selected_subject)
100
 
101
+ # --- PREVIEW SECTION ---
102
  preview_folder_path = preview_folders[selected_subject]
103
  if os.path.exists(preview_folder_path):
104
  preview_text = get_text_files_content(preview_folder_path)
 
107
  else:
108
  st.error(f"No preview available for {selected_subject}.")
109
 
110
+ # --- FULL NOTES SECTION ---
111
  subject_folder_path = subject_folders[selected_subject]
112
  if os.path.exists(subject_folder_path):
113
  raw_text = get_text_files_content(subject_folder_path)
 
115
  st.subheader("Notes")
116
  st.text_area("Notes Content:", raw_text, height=500, disabled=True)
117
 
118
+ # Button to mark as completed
119
  if selected_subject not in st.session_state.completed_subjects:
120
  if st.button(f"Mark '{selected_subject}' as Read"):
121
  st.session_state.completed_subjects.append(selected_subject)
122
  st.success(f"Marked '{selected_subject}' as read!")
123
+ # Re-run to immediately update progress
124
  st.experimental_rerun()
125
 
126
+ # Check if all notes have been read
127
  if len(st.session_state.completed_subjects) == len(subjects):
128
  st.session_state.grading_enabled = True
129
  st.sidebar.success("All subjects completed. You can now proceed to the Grading Tool.")
130
  st.markdown("[Proceed to Grading Tool](https://thecssedge.com/wp-admin/admin.php?page=feedback-dashboard)")
131
 
132
+ # Button to go to next subject
133
  if st.session_state.current_subject_index < len(subjects) - 1:
134
  if st.button("Go to Next Note"):
135
  st.session_state.current_subject_index += 1