EdBoy2202 commited on
Commit
3bc5a71
·
verified ·
1 Parent(s): 21c7278

Fixing sidebar

Browse files
Files changed (1) hide show
  1. app.py +33 -29
app.py CHANGED
@@ -11,7 +11,7 @@ st.set_page_config(initial_sidebar_state="collapsed")
11
  def login_page():
12
  # st.title("API Selection")
13
 
14
-
15
  api_options = ["Gemini API", "OpenAI API"]
16
  selected_api = st.selectbox("### Select API:", api_options)
17
  api_key = st.text_input(f"Enter your {selected_api} Key:", type="password")
@@ -45,7 +45,7 @@ def login_page():
45
  def quiz_app():
46
  st.title("QuizLM 🧠")
47
 
48
- # Initialize session state for quiz data and progress (if not already initialized during login)
49
  if 'quiz_data' not in st.session_state:
50
  st.session_state.quiz_data = None
51
  if 'current_question_index' not in st.session_state:
@@ -56,8 +56,11 @@ def quiz_app():
56
  st.session_state.quiz_completed = False
57
  if 'score' not in st.session_state:
58
  st.session_state.score = 0
59
- if 'quiz_started' not in st.session_state: # Track if quiz generation has started
60
  st.session_state.quiz_started = False
 
 
 
61
 
62
  # API Configuration based on selection - Moved inside quiz_app after login
63
  if st.session_state.selected_api == "Gemini API":
@@ -202,20 +205,16 @@ def quiz_app():
202
  st.markdown(f"<font color='{color}'>{result_text}</font>", unsafe_allow_html=True)
203
 
204
  st.divider()
205
-
206
  percentage_correct = (correct_count / len(st.session_state.quiz_data)) * 100
207
-
208
  if percentage_correct > 75:
209
  border_color = "#9AD8E1"
210
  delta_text_color = "normal"
211
  else:
212
  border_color = "#E098B7"
213
  delta_text_color = "inverse"
214
-
215
- # if st.get_option("theme.base") == "dark":
216
- # background_color = "#292D34" # Dark background for dark mode
217
- # else:
218
- # background_color = "#FFF" # White background for light mode
219
 
220
  st.write(
221
  """
@@ -227,24 +226,27 @@ def quiz_app():
227
  """,
228
  unsafe_allow_html=True,
229
  )
230
-
231
  st.metric(label=f"**Final Score**", value=f"{correct_count} out of {len(st.session_state.quiz_data)}", delta=f"{percentage_correct:.0f}%", delta_color=delta_text_color)
232
  style_metric_cards(border_left_color=border_color, background_color="DEFAULT_BACKGROUND_COLOR")
233
-
234
- # st.markdown(f"### Final Score: {correct_count} out of {len(st.session_state.quiz_data)} correct ({percentage_correct:.2f}%)")
235
  st.session_state.score = correct_count # Store score in session state
236
 
237
 
238
- # User input for topic - Conditional placement
239
- if not st.session_state.quiz_started:
240
- topic = st.text_input("Enter the topic for your quiz:")
241
- else:
242
- topic = st.sidebar.text_input("Quiz Topic", value=st.session_state.get('quiz_topic', '')) # Move to sidebar after quiz starts, keep topic value
243
 
244
- if topic and not st.session_state.quiz_started: # Only process topic and button if quiz hasn't started yet for this topic
245
- if st.button("Generate Quiz"):
246
- st.session_state.quiz_started = True # Set quiz started to true when generate button is clicked
247
- st.session_state.quiz_topic = topic # Store topic in session state to keep it in sidebar input
 
 
 
 
 
 
 
248
  with st.spinner(f"Generating quiz on '{topic}'..."):
249
  try:
250
  prompt = f"""
@@ -307,10 +309,6 @@ def quiz_app():
307
  if quiz_content: # Check if quiz_content was generated successfully (outer if)
308
  if parsed_quiz_data: # Check if parsing was successful (inner if)
309
  st.session_state.quiz_data = parsed_quiz_data
310
- st.session_state.current_question_index = 0
311
- st.session_state.user_answers = []
312
- st.session_state.quiz_completed = False
313
- st.session_state.score = 0
314
  st.success(f"Quiz on '{topic}' generated successfully! Let's begin.")
315
  else: # else associated with inner if parsed_quiz_data
316
  st.error("Failed to parse quiz content. Please try generating again.")
@@ -321,7 +319,10 @@ def quiz_app():
321
  except Exception as e:
322
  st.error(f"An error occurred: {e}")
323
  st.error("Please check your API key and network connection. If the problem persists, try a different topic or try again later.")
324
- st.rerun() # Rerun to move topic input to sidebar
 
 
 
325
 
326
  # Quiz Display Logic
327
  if st.session_state.quiz_data:
@@ -329,8 +330,11 @@ def quiz_app():
329
  display_question()
330
  else:
331
  display_results()
332
- elif topic and not st.session_state.quiz_data and not st.session_state.quiz_completed and not st.session_state.quiz_started: # Message if topic entered but quiz not generated yet and quiz not started
333
- st.info("Click 'Generate Quiz' to start the quiz.")
 
 
 
334
 
335
  # --- Main App Flow Control ---
336
  if 'logged_in' not in st.session_state:
 
11
  def login_page():
12
  # st.title("API Selection")
13
 
14
+
15
  api_options = ["Gemini API", "OpenAI API"]
16
  selected_api = st.selectbox("### Select API:", api_options)
17
  api_key = st.text_input(f"Enter your {selected_api} Key:", type="password")
 
45
  def quiz_app():
46
  st.title("QuizLM 🧠")
47
 
48
+ # Initialize session state for quiz data and progress
49
  if 'quiz_data' not in st.session_state:
50
  st.session_state.quiz_data = None
51
  if 'current_question_index' not in st.session_state:
 
56
  st.session_state.quiz_completed = False
57
  if 'score' not in st.session_state:
58
  st.session_state.score = 0
59
+ if 'quiz_started' not in st.session_state:
60
  st.session_state.quiz_started = False
61
+ if 'quiz_topic' not in st.session_state:
62
+ st.session_state.quiz_topic = "" # Initialize quiz topic
63
+
64
 
65
  # API Configuration based on selection - Moved inside quiz_app after login
66
  if st.session_state.selected_api == "Gemini API":
 
205
  st.markdown(f"<font color='{color}'>{result_text}</font>", unsafe_allow_html=True)
206
 
207
  st.divider()
208
+
209
  percentage_correct = (correct_count / len(st.session_state.quiz_data)) * 100
210
+
211
  if percentage_correct > 75:
212
  border_color = "#9AD8E1"
213
  delta_text_color = "normal"
214
  else:
215
  border_color = "#E098B7"
216
  delta_text_color = "inverse"
217
+
 
 
 
 
218
 
219
  st.write(
220
  """
 
226
  """,
227
  unsafe_allow_html=True,
228
  )
229
+
230
  st.metric(label=f"**Final Score**", value=f"{correct_count} out of {len(st.session_state.quiz_data)}", delta=f"{percentage_correct:.0f}%", delta_color=delta_text_color)
231
  style_metric_cards(border_left_color=border_color, background_color="DEFAULT_BACKGROUND_COLOR")
232
+
 
233
  st.session_state.score = correct_count # Store score in session state
234
 
235
 
236
+ # User input for topic in sidebar
237
+ topic = st.sidebar.text_input("Quiz Topic", value=st.session_state.get('quiz_topic', ''))
 
 
 
238
 
239
+ if st.sidebar.button("Generate Quiz"): # Button in sidebar to generate quiz
240
+ st.session_state.quiz_started = True # Set quiz started to true when generate button is clicked
241
+ st.session_state.quiz_topic = topic # Store topic in session state to keep it in sidebar input
242
+ # Reset quiz state when generating a new quiz
243
+ st.session_state.quiz_data = None
244
+ st.session_state.current_question_index = 0
245
+ st.session_state.user_answers = []
246
+ st.session_state.quiz_completed = False
247
+ st.session_state.score = 0
248
+
249
+ if topic:
250
  with st.spinner(f"Generating quiz on '{topic}'..."):
251
  try:
252
  prompt = f"""
 
309
  if quiz_content: # Check if quiz_content was generated successfully (outer if)
310
  if parsed_quiz_data: # Check if parsing was successful (inner if)
311
  st.session_state.quiz_data = parsed_quiz_data
 
 
 
 
312
  st.success(f"Quiz on '{topic}' generated successfully! Let's begin.")
313
  else: # else associated with inner if parsed_quiz_data
314
  st.error("Failed to parse quiz content. Please try generating again.")
 
319
  except Exception as e:
320
  st.error(f"An error occurred: {e}")
321
  st.error("Please check your API key and network connection. If the problem persists, try a different topic or try again later.")
322
+ else:
323
+ st.warning("Please enter a topic to generate a quiz.")
324
+ st.rerun() # Rerun to update the quiz display based on new topic/generation
325
+
326
 
327
  # Quiz Display Logic
328
  if st.session_state.quiz_data:
 
330
  display_question()
331
  else:
332
  display_results()
333
+ elif topic and not st.session_state.quiz_data and not st.session_state.quiz_completed and st.session_state.quiz_started == False: # Message if topic entered but quiz not generated yet and quiz not started
334
+ st.info("Enter a topic and click 'Generate Quiz' in the sidebar to start.")
335
+ elif not topic and not st.session_state.quiz_data and not st.session_state.quiz_completed and st.session_state.quiz_started == False:
336
+ st.info("Enter a topic in the sidebar to generate a quiz.")
337
+
338
 
339
  # --- Main App Flow Control ---
340
  if 'logged_in' not in st.session_state: