emvecchi commited on
Commit
50528e4
·
verified ·
1 Parent(s): e5b248c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -157
app.py CHANGED
@@ -486,165 +486,166 @@ def navigate(index_change):
486
  )
487
  st.rerun()
488
 
489
- '''
490
- def show_field(f: Field, index: int, data_collected):
491
- if f.type not in INPUT_FIELD_DEFAULT_VALUES.keys():
492
- st.session_state.following_mandatory = False
493
- match f.type:
494
- case 'input_col':
495
- #if f.name and f.name in st.session_state.data.columns:
496
- # value = st.session_state.data.iloc[index][f.name]
497
- #else:
498
- # value = None
499
- value = st.session_state.data.iloc[index][f.name]
500
- if value and value is not np.nan:
501
- st.write(f.title)
502
- if f.name == 'image_name':
503
- display_image(os.path.join(input_repo_path, 'images', value))
504
- elif f.name == 'dialogue_name':
505
- render_dialogue(os.path.join(input_repo_path, 'dialogues', value), width_chars=115, height_px=520, show_border=False)
506
- #elif f.name == 'rupture_marker_overview':
507
- # display_dialogue(os.path.join(input_repo_path, 'rupture_marker_overview.md'))
508
- elif f.name == 'patient':
509
- st.markdown(f"## Patient:  {value}")
510
- else:
511
- st.write(value)
512
- case 'markdown':
513
- path = f.other_params.get("path") if f.other_params else None
514
- if path:
515
- content = load_text(os.path.join(input_repo_path, path))
516
- st.markdown(content, unsafe_allow_html=True)
517
- else:
518
- st.markdown(f.title)
519
- case 'expander' | 'container':
520
- with (st.expander(f.title) if f.type == 'expander' else st.container(border=True)):
521
- if f.type == 'container':
522
- st.markdown(f.title)
523
- for child in f.children:
524
- show_field(child, index, data_collected)
525
- case 'skip_checkbox':
526
- st.checkbox(f.title, key=f.name, value=False)
527
- case 'rupture_markers':
528
- render_rupture_markers_widget(index, rupture_choices)
529
- else:
530
- key = f.name + str(index)
531
- st.session_state.data_inputs_keys.append(f.name)
532
- value = st.session_state[key] if key in st.session_state else \
533
- (data_collected[f.name] if data_collected else INPUT_FIELD_DEFAULT_VALUES[f.type])
534
- if not SHOW_HELP_ICON:
535
- f.title = f'**{f.title}**\n\n{f.help}' if f.help else f.title
536
-
537
- validation_error = False
538
-
539
- # form is not displayed for first time
540
- if st.session_state.form_displayed == st.session_state.current_index:
541
- if st.session_state.following_mandatory and f.skip_mandatory:
542
- st.session_state.following_mandatory = False
543
- if f.following_mandatory_values and st.session_state[key] in f.following_mandatory_values:
544
- st.session_state.following_mandatory = True
545
- if f.mandatory or st.session_state.following_mandatory:
546
- if st.session_state[key] == INPUT_FIELD_DEFAULT_VALUES[f.type]:
547
- st.session_state.valid = False
548
- validation_error = True
549
- elif f.following_mandatory_values and st.session_state[key] in f.following_mandatory_values:
550
- st.session_state.following_mandatory = True
551
-
552
- # check for any unaccepted values
553
- if (
554
- (f.other_params.get('accepted_values') and
555
- value not in f.other_params.get('accepted_values')) or
556
- (f.other_params.get('accepted_values_per_sample') and
557
- index in f.other_params.get('accepted_values_per_sample') and
558
- value not in f.other_params.get('accepted_values_per_sample').get(index))
559
- ):
560
- st.session_state.unacceptable_response = True
561
-
562
- if f.mandatory or st.session_state.following_mandatory:
563
- f.title += " :red[* required!]" if (validation_error and not SHOW_VALIDATION_ERROR_MESSAGE) else' :red[*]'
564
- f.help = None
565
-
566
- match f.type:
567
- case 'checkbox':
568
- st.checkbox(f.title,
569
- key=key,
570
- value=value, help=f.help)
571
- case 'radio':
572
- labels = default_labels if not f.other_params.get('labels') else f.other_params.get('labels')
573
- st.radio(f.title,
574
- options=range(len(labels)),
575
- format_func=lambda x: labels[x],
576
- key=key,
577
- index=value, help=f.help, horizontal=False)
578
- case 'slider':
579
- st.slider(f.title,
580
- min_value=0, max_value=6, step=1,
581
- key=key,
582
- value=value, help=f.help)
583
- case 'select_slider':
584
- labels = default_labels if not f.other_params.get('labels') else f.other_params.get('labels')
585
- st.select_slider(f.title,
586
- options=[0, 20, 40, 60, 80, 100],
587
- format_func=lambda x: labels[x // 20],
588
- key=key,
589
- value=value, help=f.help)
590
- case 'multiselect':
591
- choices = default_choices if not f.other_params.get('choices') else f.other_params.get('choices')
592
- st.multiselect(f.title,
593
- options = choices,
594
- format_func=lambda x: x,
595
- key=key, max_selections=3,
596
- default=value, help=f.help)
597
- case 'likert_radio':
598
- labels = default_labels if not f.other_params.get('labels') else f.other_params.get('labels')
599
- st.radio(f.title,
600
- options=[0, 1, 2, 3, 4],
601
- format_func=lambda x: labels[x],
602
- key=key,
603
- index=value, help=f.help, horizontal=True)
604
- case 'y_n_radio':
605
- labels = yes_no_labels if not f.other_params.get('labels') else f.other_params.get('labels')
606
- st.radio(f.title,
607
- options=[0, 1],
608
- format_func=lambda x: labels[x],
609
- key=key,
610
- index=value, help=f.help, horizontal=True)
611
- case 'text':
612
- st.text_input(f.title, key=key, value=value, max_chars=None)
613
- case 'textarea':
614
- st.text_area(f.title, key=key, value=value, max_chars=None)
615
-
616
- if validation_error:
617
- st.session_state.unacceptable_response = False
618
- st.error(f"Mandatory field")
619
-
620
-
621
- def show_fields(fields: List[Field]):
622
- st.session_state.valid = True
623
- index = st.session_state.current_index
624
- data_collected = read_saved_data()
625
- st.session_state.data_inputs_keys = []
626
- st.session_state.following_mandatory = False
627
 
628
- for field in fields:
629
- show_field(field, index, data_collected)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
630
 
631
- submitted = st.form_submit_button("Submit")
632
- if submitted:
633
- if 'unacceptable_response' in st.session_state and st.session_state.unacceptable_response:
634
- prep_and_save_data(index, ('skip' in st.session_state and st.session_state['skip']))
635
- st.rerun()
636
- skip_sample = ('skip' in st.session_state and st.session_state['skip'])
637
- if not skip_sample and not st.session_state.valid:
638
- st.error("Please fill in all mandatory fields")
639
- # st.rerun() # filed-out values are not shown otherwise
640
- else:
641
- with st.spinner(text="saving"):
642
- prep_and_save_data(index, skip_sample)
643
- st.success("Feedback submitted successfully!")
644
- navigate(1)
645
-
646
- st.session_state.form_displayed = st.session_state.current_index
647
- '''
648
  def show_field(f: Field, index: int, data_collected):
649
  """Render a Field tree. Pure rendering (no form submit logic here)."""
650
 
 
486
  )
487
  st.rerun()
488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
 
490
+ #def show_field(f: Field, index: int, data_collected):
491
+ # if f.type not in INPUT_FIELD_DEFAULT_VALUES.keys():
492
+ # st.session_state.following_mandatory = False
493
+ # match f.type:
494
+ # case 'input_col':
495
+ # #if f.name and f.name in st.session_state.data.columns:
496
+ # # value = st.session_state.data.iloc[index][f.name]
497
+ # #else:
498
+ # # value = None
499
+ # value = st.session_state.data.iloc[index][f.name]
500
+ # if value and value is not np.nan:
501
+ # st.write(f.title)
502
+ # if f.name == 'image_name':
503
+ # display_image(os.path.join(input_repo_path, 'images', value))
504
+ # elif f.name == 'dialogue_name':
505
+ # render_dialogue(os.path.join(input_repo_path, 'dialogues', value), width_chars=115, height_px=520, show_border=False)
506
+ # #elif f.name == 'rupture_marker_overview':
507
+ # # display_dialogue(os.path.join(input_repo_path, 'rupture_marker_overview.md'))
508
+ # elif f.name == 'patient':
509
+ # st.markdown(f"## Patient:  {value}")
510
+ # else:
511
+ # st.write(value)
512
+ # case 'markdown':
513
+ # path = f.other_params.get("path") if f.other_params else None
514
+ # if path:
515
+ # content = load_text(os.path.join(input_repo_path, path))
516
+ # st.markdown(content, unsafe_allow_html=True)
517
+ # else:
518
+ # st.markdown(f.title)
519
+ # case 'expander' | 'container':
520
+ # with (st.expander(f.title) if f.type == 'expander' else st.container(border=True)):
521
+ # if f.type == 'container':
522
+ # st.markdown(f.title)
523
+ # for child in f.children:
524
+ # show_field(child, index, data_collected)
525
+ # case 'skip_checkbox':
526
+ # st.checkbox(f.title, key=f.name, value=False)
527
+ # case 'rupture_markers':
528
+ # render_rupture_markers_widget(index, rupture_choices)
529
+ # else:
530
+ # key = f.name + str(index)
531
+ # st.session_state.data_inputs_keys.append(f.name)
532
+ # value = st.session_state[key] if key in st.session_state else \
533
+ # (data_collected[f.name] if data_collected else INPUT_FIELD_DEFAULT_VALUES[f.type])
534
+ # if not SHOW_HELP_ICON:
535
+ # f.title = f'**{f.title}**\n\n{f.help}' if f.help else f.title
536
+ #
537
+ # validation_error = False
538
+ #
539
+ # # form is not displayed for first time
540
+ # if st.session_state.form_displayed == st.session_state.current_index:
541
+ # if st.session_state.following_mandatory and f.skip_mandatory:
542
+ # st.session_state.following_mandatory = False
543
+ # if f.following_mandatory_values and st.session_state[key] in f.following_mandatory_values:
544
+ # st.session_state.following_mandatory = True
545
+ # if f.mandatory or st.session_state.following_mandatory:
546
+ # if st.session_state[key] == INPUT_FIELD_DEFAULT_VALUES[f.type]:
547
+ # st.session_state.valid = False
548
+ # validation_error = True
549
+ # elif f.following_mandatory_values and st.session_state[key] in f.following_mandatory_values:
550
+ # st.session_state.following_mandatory = True
551
+ #
552
+ # # check for any unaccepted values
553
+ # if (
554
+ # (f.other_params.get('accepted_values') and
555
+ # value not in f.other_params.get('accepted_values')) or
556
+ # (f.other_params.get('accepted_values_per_sample') and
557
+ # index in f.other_params.get('accepted_values_per_sample') and
558
+ # value not in f.other_params.get('accepted_values_per_sample').get(index))
559
+ # ):
560
+ # st.session_state.unacceptable_response = True
561
+ #
562
+ # if f.mandatory or st.session_state.following_mandatory:
563
+ # f.title += " :red[* required!]" if (validation_error and not SHOW_VALIDATION_ERROR_MESSAGE) else' :red[*]'
564
+ # f.help = None
565
+ #
566
+ # match f.type:
567
+ # case 'checkbox':
568
+ # st.checkbox(f.title,
569
+ # key=key,
570
+ # value=value, help=f.help)
571
+ # case 'radio':
572
+ # labels = default_labels if not f.other_params.get('labels') else f.other_params.get('labels')
573
+ # st.radio(f.title,
574
+ # options=range(len(labels)),
575
+ # format_func=lambda x: labels[x],
576
+ # key=key,
577
+ # index=value, help=f.help, horizontal=False)
578
+ # case 'slider':
579
+ # st.slider(f.title,
580
+ # min_value=0, max_value=6, step=1,
581
+ # key=key,
582
+ # value=value, help=f.help)
583
+ # case 'select_slider':
584
+ # labels = default_labels if not f.other_params.get('labels') else f.other_params.get('labels')
585
+ # st.select_slider(f.title,
586
+ # options=[0, 20, 40, 60, 80, 100],
587
+ # format_func=lambda x: labels[x // 20],
588
+ # key=key,
589
+ # value=value, help=f.help)
590
+ # case 'multiselect':
591
+ # choices = default_choices if not f.other_params.get('choices') else f.other_params.get('choices')
592
+ # st.multiselect(f.title,
593
+ # options = choices,
594
+ # format_func=lambda x: x,
595
+ # key=key, max_selections=3,
596
+ # default=value, help=f.help)
597
+ # case 'likert_radio':
598
+ # labels = default_labels if not f.other_params.get('labels') else f.other_params.get('labels')
599
+ # st.radio(f.title,
600
+ # options=[0, 1, 2, 3, 4],
601
+ # format_func=lambda x: labels[x],
602
+ # key=key,
603
+ # index=value, help=f.help, horizontal=True)
604
+ # case 'y_n_radio':
605
+ # labels = yes_no_labels if not f.other_params.get('labels') else f.other_params.get('labels')
606
+ # st.radio(f.title,
607
+ # options=[0, 1],
608
+ # format_func=lambda x: labels[x],
609
+ # key=key,
610
+ # index=value, help=f.help, horizontal=True)
611
+ # case 'text':
612
+ # st.text_input(f.title, key=key, value=value, max_chars=None)
613
+ # case 'textarea':
614
+ # st.text_area(f.title, key=key, value=value, max_chars=None)
615
+ #
616
+ # if validation_error:
617
+ # st.session_state.unacceptable_response = False
618
+ # st.error(f"Mandatory field")
619
+
620
+
621
+ #def show_fields(fields: List[Field]):
622
+ # st.session_state.valid = True
623
+ # index = st.session_state.current_index
624
+ # data_collected = read_saved_data()
625
+ # st.session_state.data_inputs_keys = []
626
+ # st.session_state.following_mandatory = False
627
+ #
628
+ # for field in fields:
629
+ # show_field(field, index, data_collected)
630
+ #
631
+ # submitted = st.form_submit_button("Submit")
632
+ # if submitted:
633
+ # if 'unacceptable_response' in st.session_state and st.session_state.unacceptable_response:
634
+ # prep_and_save_data(index, ('skip' in st.session_state and st.session_state['skip']))
635
+ # st.rerun()
636
+ # skip_sample = ('skip' in st.session_state and st.session_state['skip'])
637
+ # if not skip_sample and not st.session_state.valid:
638
+ # st.error("Please fill in all mandatory fields")
639
+ # # st.rerun() # filed-out values are not shown otherwise
640
+ # else:
641
+ # with st.spinner(text="saving"):
642
+ # prep_and_save_data(index, skip_sample)
643
+ # st.success("Feedback submitted successfully!")
644
+ # navigate(1)
645
+ #
646
+ # st.session_state.form_displayed = st.session_state.current_index
647
+ #
648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
649
  def show_field(f: Field, index: int, data_collected):
650
  """Render a Field tree. Pure rendering (no form submit logic here)."""
651