GSMEthesis commited on
Commit
00299f1
·
verified ·
1 Parent(s): 0fb137e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -194
app.py CHANGED
@@ -739,7 +739,7 @@ h3 {
739
 
740
  # ========== توابع اصلی ==========
741
  def enhanced_likert_scale(question_data):
742
- """لیکرت اسکیل با دکمه‌های Streamlit مخفی دقیقاً زیر دکمه‌های HTML"""
743
  question = question_data["question"]
744
  key = question_data["key"]
745
  scale = question_data["scale"]
@@ -749,204 +749,126 @@ def enhanced_likert_scale(question_data):
749
  if key not in st.session_state:
750
  st.session_state[key] = None
751
 
752
- # استایل‌دهی و ساختار HTML
753
- html_content = f"""
754
- <style>
755
- /* استایل‌های کانتینر لیکرت */
756
- .likert-container {{
757
- font-family: 'B Nazanin', Arial, sans-serif !important;
758
- direction: rtl !important;
759
- text-align: center !important;
760
- margin: 0 !important;
761
- position: relative !important;
762
- }}
763
- .likert-question {{
764
- font-size: 18px !important;
765
- font-weight: bold !important;
766
- margin-bottom: 15px !important;
767
- color: #333333 !important;
768
- }}
769
- .likert-labels {{
770
- display: flex !important;
771
- justify-content: space-between !important;
772
- font-size: 14px !important;
773
- margin-bottom: 10px !important;
774
- color: #666666 !important;
775
- }}
776
- .likert-buttons {{
777
- display: flex !important;
778
- justify-content: center !important;
779
- gap: 8px !important;
780
- margin: 0 !important;
781
- padding: 0 !important;
782
- position: relative !important;
783
- height: 40px !important;
784
- }}
785
- /* استایل دکمه‌های لیکرت (HTML) */
786
- .likert-btn {{
787
- position: absolute !important;
788
- width: 40px !important;
789
- height: 40px !important;
790
- border-radius: 50% !important;
791
- border: 2px solid #6a0dad !important;
792
- background: white !important;
793
- display: flex !important;
794
- align-items: center !important;
795
- justify-content: center !important;
796
- cursor: pointer !important;
797
- transition: all 0.3s ease !important;
798
- font-size: 18px !important;
799
- color: #6a0dad !important;
800
- z-index: 1000 !important;
801
- }}
802
- .likert-btn.selected {{
803
- background: #6a0dad !important;
804
- color: white !important;
805
- box-shadow: 0 0 10px rgba(106, 13, 173, 0.5) !important;
806
- }}
807
- .likert-btn:hover {{
808
- background: #f0e6ff !important;
809
- transform: scale(1.1) !important;
810
- }}
811
- /* مخفی کردن دکمه‌های Streamlit */
812
- .likert-streamlit-btn-container {{
813
- position: relative !important;
814
- width: 40px !important;
815
- height: 40px !important;
816
- margin: 0 !important;
817
- padding: 0 !important;
818
- }}
819
- .likert-streamlit-btn {{
820
- width: 40px !important;
821
- height: 40px !important;
822
- opacity: 0 !important;
823
- position: absolute !important;
824
- top: 0 !important;
825
- left: 0 !important;
826
- margin: 0 !important;
827
- padding: 0 !important;
828
- border: none !important;
829
- background: white !important;
830
- z-index: 1 !important;
831
- }}
832
- .likert-streamlit-btn:hover,
833
- .likert-streamlit-btn:focus,
834
- .likert-streamlit-btn:active {{
835
- border: none !important;
836
- background: white !important;
837
- }}
838
- /* استایل پاسخ انتخاب‌شده */
839
- .likert-response {{
840
- text-align: center !important;
841
- color: #6a0dad !important;
842
- font-weight: bold !important;
843
- margin-top: 10px !important;
844
- font-size: 16px !important;
845
- }}
846
- </style>
847
- <div class="likert-container">
848
- <div class="likert-question">{question}</div>
849
- <div class="likert-labels">
850
- <span style="text-align:right">{labels[0]}</span>
851
- <span style="text-align:left">{labels[1]}</span>
852
- </div>
853
- <div class="likert-buttons" id="likert-buttons-{key}">
854
- """
855
 
856
- # محاسبه موقعیت دکمه‌ها
857
- button_width = 40
858
- gap = 8
859
- total_width = scale * button_width + (scale - 1) * gap
860
- start_x = -total_width / 2 # برای وسط‌چین کردن دکمه‌ها
861
 
862
- # افزودن دکمه‌های HTML
863
- for i in range(scale):
864
- value = scale - i # 7 تا 1
865
- is_selected = st.session_state[key] == value
866
- x_position = start_x + i * (button_width + gap)
867
- html_content += f"""
868
- <button class="likert-btn {'selected' if is_selected else ''}"
869
- style="left: calc(50% + {x_position}px); top: 0;"
870
- onclick="document.getElementById('streamlit-btn-{key}-{value}').click()">
871
- {'•' if is_selected else '○'}
872
- </button>
873
- """
874
 
875
- html_content += f"""
876
- </div>
877
- <div class="likert-response" id="likert-response-{key}">
878
- پاسخ شما: {st.session_state[key] or "هیچکدام"}
879
- </div>
880
- </div>
881
- <script>
882
- function updateLikertSelection(key, value) {{
883
- const buttons = document.querySelectorAll('#likert-buttons-{key} .likert-btn');
884
- buttons.forEach(btn => btn.classList.remove('selected'));
885
- const selectedBtn = Array.from(buttons).find(btn => btn.onclick.toString().includes('streamlit-btn-{key}-' + value));
886
- if (selectedBtn) selectedBtn.classList.add('selected');
887
- document.getElementById('likert-response-{key}').innerText = 'پاسخ شما: ' + (value || '��یچکدام');
888
- }}
889
- </script>
890
- """
891
 
892
- # رندر HTML
893
- components.html(html_content, height=150)
 
 
 
894
 
895
- # ایجاد دکمه‌های Streamlit
896
- st.markdown('<div style="display: flex; justify-content: center; gap: 8px; margin: 0; padding: 0;">', unsafe_allow_html=True)
897
- for i in range(scale):
898
- value = scale - i # 7 تا 1
899
- st.markdown(
900
- f"""
901
- <style>
902
- .likert-streamlit-btn-container-{key}-{value} {{
903
- position: relative !important;
904
- width: 40px !important;
905
- height: 40px !important;
906
- margin: 0 !important;
907
- padding: 0 !important;
908
- }}
909
- #streamlit-btn-{key}-{value} {{
910
- width: 40px !important;
911
- height: 40px !important;
912
- opacity: 0 !important;
913
- position: absolute !important;
914
- top: 0 !important;
915
- left: 0 !important;
916
- margin: 0 !important;
917
- padding: 0 !important;
918
- border: none !important;
919
- background: white !important;
920
- z-index: 1 !important;
921
- }}
922
- #streamlit-btn-{key}-{value}:hover,
923
- #streamlit-btn-{key}-{value}:focus,
924
- #streamlit-btn-{key}-{value}:active {{
925
- border: none !important;
926
- background: white !important;
927
- }}
928
- </style>
929
- <div class="likert-streamlit-btn-container-{key}-{value}"></div>
930
- """,
931
- unsafe_allow_html=True
932
- )
933
- if st.button(
934
- "•" if st.session_state[key] == value else "○",
935
- key=f"streamlit-btn-{key}-{value}",
936
- help=str(value),
937
- type="secondary"
938
- ):
939
- st.session_state[key] = value
940
- # آپدیت دکمه‌های HTML با JavaScript
941
- st.markdown(
942
- f"""
943
- <script>
944
- updateLikertSelection('{key}', {value});
945
- </script>
946
- """,
947
- unsafe_allow_html=True
948
- )
949
- st.rerun()
950
  st.markdown('</div>', unsafe_allow_html=True)
951
 
952
  return st.session_state.get(key)
 
739
 
740
  # ========== توابع اصلی ==========
741
  def enhanced_likert_scale(question_data):
742
+ """لیکرت اسکیل فقط با فیچرهای Streamlit و دکمه‌های دایره‌ای ریسپانسیو"""
743
  question = question_data["question"]
744
  key = question_data["key"]
745
  scale = question_data["scale"]
 
749
  if key not in st.session_state:
750
  st.session_state[key] = None
751
 
752
+ # استایل‌دهی سفارشی برای دکمه‌های Streamlit
753
+ st.markdown(
754
+ f"""
755
+ <style>
756
+ /* استایل کانتینر لیکرت */
757
+ .likert-container {{
758
+ font-family: 'B Nazanin', Arial, sans-serif !important;
759
+ direction: rtl !important;
760
+ text-align: center !important;
761
+ margin: 0 !important;
762
+ }}
763
+ .likert-question {{
764
+ font-size: 18px !important;
765
+ font-weight: bold !important;
766
+ margin-bottom: 15px !important;
767
+ color: #333333 !important;
768
+ }}
769
+ .likert-labels {{
770
+ display: flex !important;
771
+ justify-content: space-between !important;
772
+ font-size: 14px !important;
773
+ margin-bottom: 10px !important;
774
+ color: #666666 !important;
775
+ }}
776
+ /* استایل ردیف دکمه‌ها */
777
+ .likert-buttons {{
778
+ display: flex !important;
779
+ justify-content: center !important;
780
+ gap: 8px !important;
781
+ flex-wrap: wrap !important;
782
+ margin: 0 !important;
783
+ padding: 0 !important;
784
+ }}
785
+ /* استایل دکمه‌های Streamlit */
786
+ div.stButton > button[key^="streamlit-btn-{key}-"] {{
787
+ width: 40px !important;
788
+ height: 40px !important;
789
+ border-radius: 50% !important;
790
+ border: 2px solid #6a0dad !important;
791
+ background: white !important;
792
+ display: flex !important;
793
+ align-items: center !important;
794
+ justify-content: center !important;
795
+ font-size: 18px !important;
796
+ color: #6a0dad !important;
797
+ padding: 0 !important;
798
+ margin: 0 !important;
799
+ transition: all 0.3s ease !important;
800
+ }}
801
+ div.stButton > button[key^="streamlit-btn-{key}-"]:hover {{
802
+ background: #f0e6ff !important;
803
+ transform: scale(1.1) !important;
804
+ }}
805
+ div.stButton > button[key^="streamlit-btn-{key}-"]:focus,
806
+ div.stButton > button[key^="streamlit-btn-{key}-"]:active {{
807
+ background: #6a0dad !important;
808
+ color: white !important;
809
+ box-shadow: 0 0 10px rgba(106, 13, 173, 0.5) !important;
810
+ }}
811
+ /* استایل دکمه انتخابشده */
812
+ div.stButton > button[key="streamlit-btn-{key}-{st.session_state[key] if st.session_state[key] else 'none'}"] {{
813
+ background: #6a0dad !important;
814
+ color: white !important;
815
+ box-shadow: 0 0 10px rgba(106, 13, 173, 0.5) !important;
816
+ }}
817
+ /* استایل پاسخ */
818
+ .likert-response {{
819
+ text-align: center !important;
820
+ color: #6a0dad !important;
821
+ font-weight: bold !important;
822
+ margin-top: 10px !important;
823
+ font-size: 16px !important;
824
+ }}
825
+ /* ریسپانسیو کردن */
826
+ @media (max-width: 600px) {{
827
+ .likert-buttons {{
828
+ gap: 4px !important;
829
+ }}
830
+ div.stButton > button[key^="streamlit-btn-{key}-"] {{
831
+ width: 36px !important;
832
+ height: 36px !important;
833
+ font-size: 16px !important;
834
+ }}
835
+ }}
836
+ </style>
837
+ """,
838
+ unsafe_allow_html=True
839
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
840
 
841
+ # نمایش سوال
842
+ st.markdown(f'<div class="likert-container"><div class="likert-question">{question}</div>', unsafe_allow_html=True)
 
 
 
843
 
844
+ # نمایش لیبل‌ها
845
+ st.markdown(
846
+ f'<div class="likert-labels"><span style="text-align:right">{labels[0]}</span><span style="text-align:left">{labels[1]}</span></div>',
847
+ unsafe_allow_html=True
848
+ )
 
 
 
 
 
 
 
849
 
850
+ # نمایش دکمه‌ها در یک ردیف
851
+ st.markdown('<div class="likert-buttons">', unsafe_allow_html=True)
852
+ cols = st.columns(scale)
853
+ for i in range(scale):
854
+ with cols[i]:
855
+ value = scale - i # 7 تا 1
856
+ symbol = "•" if st.session_state[key] == value else "○"
857
+ if st.button(
858
+ symbol,
859
+ key=f"streamlit-btn-{key}-{value}",
860
+ help=str(value)
861
+ ):
862
+ st.session_state[key] = value
863
+ st.rerun()
864
+ st.markdown('</div>', unsafe_allow_html=True)
 
865
 
866
+ # نمایش پاسخ
867
+ st.markdown(
868
+ f'<div class="likert-response">پاسخ شما: {st.session_state[key] or "هیچکدام"}</div>',
869
+ unsafe_allow_html=True
870
+ )
871
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
872
  st.markdown('</div>', unsafe_allow_html=True)
873
 
874
  return st.session_state.get(key)