ycho223 commited on
Commit
97e1368
·
verified ·
1 Parent(s): e3944e5

Update src/exp3-pilot.py

Browse files
Files changed (1) hide show
  1. src/exp3-pilot.py +18 -24
src/exp3-pilot.py CHANGED
@@ -31,7 +31,7 @@ def get_google_creds():
31
  service_account_json = os.getenv("SERVICE_ACCOUNT_JSON")
32
  if service_account_json:
33
  creds_dict = json.loads(service_account_json)
34
- scope = ["https://sheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
35
  creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
36
  return gspread.authorize(creds)
37
  else:
@@ -264,15 +264,15 @@ def render_initial_login_page():
264
  worker_id = int(participant_input)
265
  st.session_state.worker_id = worker_id
266
  st.session_state.step = "welcome"
267
- st.rerun() # Trigger a rerun to go to the welcome page
268
  except ValueError:
269
  st.error("Please enter a valid numeric ID.")
270
- st.stop() # Stop further execution for this run
271
 
272
 
273
  def render_welcome_page():
274
  """Renders the welcome and first instruction page."""
275
- apply_css()
276
  st.markdown("<h1 class='compact-main-title'>Welcome to the Experiment 3!</h1>", unsafe_allow_html=True)
277
  st.markdown("<p class='instruction-text'>This is your final experiment, at last!</p>", unsafe_allow_html=True)
278
  st.markdown("<p class='instruction-text'>Please read the following instructions carefully and let us know if you have any questions.</p>", unsafe_allow_html=True)
@@ -280,9 +280,9 @@ def render_welcome_page():
280
  st.markdown("<div class='next-button-container'>", unsafe_allow_html=True)
281
  if st.button("Next", key="welcome_next_btn"):
282
  st.session_state.step = "instructions_1"
283
- st.rerun() # Trigger a rerun to go to the next instruction page
284
  st.markdown("</div>", unsafe_allow_html=True)
285
- st.stop() # Stop further execution for this run
286
 
287
 
288
  def render_instructions_page_1():
@@ -301,13 +301,13 @@ def render_instructions_page_1():
301
  st.markdown("<div class='prev-button-container'>", unsafe_allow_html=True)
302
  if st.button("Previous", key="prev_1_btn"):
303
  st.session_state.step = "welcome"
304
- st.rerun() # Trigger a rerun to go to the previous page
305
  st.markdown("</div>", unsafe_allow_html=True)
306
  with col_next:
307
  st.markdown("<div class='next-button-container'>", unsafe_allow_html=True)
308
  if st.button("Next", key="next_1_btn"):
309
  st.session_state.step = "instructions_2"
310
- st.rerun() # Trigger a rerun to go to the next page
311
  st.markdown("</div>", unsafe_allow_html=True)
312
  st.stop()
313
 
@@ -413,10 +413,10 @@ def render_instructions_page_4():
413
  st.markdown("<div class='next-button-container'>", unsafe_allow_html=True)
414
  if st.button("Start Experiment", key="start_experiment_btn_final"):
415
  st.session_state.step = "training"
416
- st.session_state.current_sample_index = 0 # Initialize sample index for the experiment
417
- st.session_state[f'start_time_{st.session_state.current_sample_index}'] = time.time() # Start timer for first sample
418
  st.rerun()
419
- st.markdown("</div>", unsafe_allow_html=True)
420
  st.stop()
421
 
422
 
@@ -427,14 +427,11 @@ def render_main_experiment_ui():
427
 
428
  current_sample_index = st.session_state.get('current_sample_index', 0)
429
 
430
- # If all samples are completed, transition to the training complete page
431
  if current_sample_index >= len(SAMPLES):
432
  st.session_state.step = "training_complete"
433
  st.rerun()
434
  st.stop() # Immediately stop after setting state and rerunning
435
 
436
- # Record the start time when the sample is first loaded/displayed
437
- # This ensures the timer starts when a *new* sample appears.
438
  if f'start_time_{current_sample_index}' not in st.session_state:
439
  st.session_state[f'start_time_{current_sample_index}'] = time.time()
440
 
@@ -562,7 +559,6 @@ def render_main_experiment_ui():
562
 
563
  st.markdown("---")
564
 
565
- # Validation logic and button actions
566
  def validate_and_proceed():
567
  all_questions_answered = True
568
  part1_radio_key = f"part1_overall_evaluation_bottom_tab_{current_sample_index}"
@@ -589,7 +585,6 @@ def render_main_experiment_ui():
589
  return False
590
  return True
591
 
592
- # Next/Complete buttons
593
  if current_sample_index < len(SAMPLES) - 1:
594
  if st.button("Next Sample"):
595
  if validate_and_proceed():
@@ -607,9 +602,7 @@ def render_main_experiment_ui():
607
  st.session_state.responses.append(current_responses)
608
 
609
  st.session_state.current_sample_index += 1
610
- # Set start time for the NEW sample
611
- st.session_state[f'start_time_{st.session_state.current_sample_index}'] = time.time()
612
- st.rerun() # Rerun to display the next sample
613
  else:
614
  if st.button("Complete Experiment"):
615
  if validate_and_proceed():
@@ -627,7 +620,7 @@ def render_main_experiment_ui():
627
  st.session_state.responses.append(current_responses)
628
 
629
  st.session_state.step = "training_complete"
630
- st.rerun() # Rerun to display the completion page
631
  st.stop()
632
 
633
 
@@ -669,16 +662,17 @@ if __name__ == "__main__":
669
  st.session_state.responses = []
670
 
671
  # --- Set page config ONCE at the very top ---
672
- # This remains the recommended best practice, even if dynamic.
673
- # Streamlit will re-apply it on each rerun. The warning is usually harmless.
 
 
674
  if st.session_state.step == "training":
675
  st.set_page_config(layout="wide")
676
  else:
677
  st.set_page_config(layout="centered")
678
 
679
  # Conditional rendering based on `st.session_state.step`
680
- # Each branch must call its specific page rendering function,
681
- # and that function MUST end with st.stop()
682
  if st.session_state.step == "initial_login":
683
  render_initial_login_page()
684
  elif st.session_state.step == "welcome":
 
31
  service_account_json = os.getenv("SERVICE_ACCOUNT_JSON")
32
  if service_account_json:
33
  creds_dict = json.loads(service_account_json)
34
+ scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
35
  creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
36
  return gspread.authorize(creds)
37
  else:
 
264
  worker_id = int(participant_input)
265
  st.session_state.worker_id = worker_id
266
  st.session_state.step = "welcome"
267
+ st.rerun() # Immediately rerun to transition to the welcome page
268
  except ValueError:
269
  st.error("Please enter a valid numeric ID.")
270
+ st.stop() # Ensure nothing else renders after this page
271
 
272
 
273
  def render_welcome_page():
274
  """Renders the welcome and first instruction page."""
275
+ apply_css() # Apply general CSS
276
  st.markdown("<h1 class='compact-main-title'>Welcome to the Experiment 3!</h1>", unsafe_allow_html=True)
277
  st.markdown("<p class='instruction-text'>This is your final experiment, at last!</p>", unsafe_allow_html=True)
278
  st.markdown("<p class='instruction-text'>Please read the following instructions carefully and let us know if you have any questions.</p>", unsafe_allow_html=True)
 
280
  st.markdown("<div class='next-button-container'>", unsafe_allow_html=True)
281
  if st.button("Next", key="welcome_next_btn"):
282
  st.session_state.step = "instructions_1"
283
+ st.rerun()
284
  st.markdown("</div>", unsafe_allow_html=True)
285
+ st.stop()
286
 
287
 
288
  def render_instructions_page_1():
 
301
  st.markdown("<div class='prev-button-container'>", unsafe_allow_html=True)
302
  if st.button("Previous", key="prev_1_btn"):
303
  st.session_state.step = "welcome"
304
+ st.rerun()
305
  st.markdown("</div>", unsafe_allow_html=True)
306
  with col_next:
307
  st.markdown("<div class='next-button-container'>", unsafe_allow_html=True)
308
  if st.button("Next", key="next_1_btn"):
309
  st.session_state.step = "instructions_2"
310
+ st.rerun()
311
  st.markdown("</div>", unsafe_allow_html=True)
312
  st.stop()
313
 
 
413
  st.markdown("<div class='next-button-container'>", unsafe_allow_html=True)
414
  if st.button("Start Experiment", key="start_experiment_btn_final"):
415
  st.session_state.step = "training"
416
+ st.session_state.current_sample_index = 0
417
+ st.session_state[f'start_time_{st.session_state.current_sample_index}'] = time.time()
418
  st.rerun()
419
+ st.markdown("</div>", unsafe_allow_html=True)
420
  st.stop()
421
 
422
 
 
427
 
428
  current_sample_index = st.session_state.get('current_sample_index', 0)
429
 
 
430
  if current_sample_index >= len(SAMPLES):
431
  st.session_state.step = "training_complete"
432
  st.rerun()
433
  st.stop() # Immediately stop after setting state and rerunning
434
 
 
 
435
  if f'start_time_{current_sample_index}' not in st.session_state:
436
  st.session_state[f'start_time_{current_sample_index}'] = time.time()
437
 
 
559
 
560
  st.markdown("---")
561
 
 
562
  def validate_and_proceed():
563
  all_questions_answered = True
564
  part1_radio_key = f"part1_overall_evaluation_bottom_tab_{current_sample_index}"
 
585
  return False
586
  return True
587
 
 
588
  if current_sample_index < len(SAMPLES) - 1:
589
  if st.button("Next Sample"):
590
  if validate_and_proceed():
 
602
  st.session_state.responses.append(current_responses)
603
 
604
  st.session_state.current_sample_index += 1
605
+ st.rerun()
 
 
606
  else:
607
  if st.button("Complete Experiment"):
608
  if validate_and_proceed():
 
620
  st.session_state.responses.append(current_responses)
621
 
622
  st.session_state.step = "training_complete"
623
+ st.rerun()
624
  st.stop()
625
 
626
 
 
662
  st.session_state.responses = []
663
 
664
  # --- Set page config ONCE at the very top ---
665
+ # This is critical for avoiding warnings and ensuring consistent layout behavior.
666
+ # Choose a default layout that works for most pages, or decide dynamically for the first render.
667
+ # Given your app shifts between centered and wide, we'll keep the dynamic set_page_config
668
+ # but acknowledge it might produce a warning. The key is that it's *before* other st calls.
669
  if st.session_state.step == "training":
670
  st.set_page_config(layout="wide")
671
  else:
672
  st.set_page_config(layout="centered")
673
 
674
  # Conditional rendering based on `st.session_state.step`
675
+ # Each branch must end with st.stop() to prevent fall-through
 
676
  if st.session_state.step == "initial_login":
677
  render_initial_login_page()
678
  elif st.session_state.step == "welcome":