GSMEthesis commited on
Commit
46440e8
·
verified ·
1 Parent(s): af58b37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -156
app.py CHANGED
@@ -739,179 +739,120 @@ h3 {
739
 
740
  # ========== توابع اصلی ==========
741
  def enhanced_likert_scale(question_data):
742
- """لیکرت اسکیل با خط فرضی، لیبل‌ها کنار، و نقاط دایره‌ای تو یه ردیف افقی"""
743
  question = question_data["question"]
744
  key = question_data["key"]
745
  scale = question_data["scale"]
746
  labels = question_data.get("labels", ["کاملاً مخالفم", "کاملاً موافقم"])
747
 
748
- # مقداردهی اولیه
749
  if key not in st.session_state:
750
  st.session_state[key] = None
751
-
752
- # استایل‌دهی سفارشی
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
- position: relative !important;
763
- }}
764
- .likert-question {{
765
- font-size: 18px !important;
766
- font-weight: bold !important;
767
- margin-bottom: 15px !important;
768
- color: #333333 !important;
769
- }}
770
- /* خط فرضی و لیبل‌ها */
771
- .likert-line-container {{
772
- position: relative !important;
773
- width: 80% !important;
774
- margin: 0 auto !important;
775
- display: flex !important;
776
- align-items: center !important;
777
- }}
778
- .likert-label-left {{
779
- position: absolute !important;
780
- left: -10% !important;
781
- font-size: 14px !important;
782
- color: #666666 !important;
783
- text-align: right !important;
784
- }}
785
- .likert-label-right {{
786
- position: absolute !important;
787
- right: -10% !important;
788
- font-size: 14px !important;
789
- color: #666666 !important;
790
- text-align: left !important;
791
- }}
792
- /* استایل دکمه‌ها */
793
- .likert-buttons {{
794
- display: flex !important;
795
- justify-content: space-between !important;
796
- gap: 10px !important;
797
- flex-wrap: nowrap !important;
798
- margin: 0 !important;
799
- padding: 0 !important;
800
- align-items: center !important;
801
- width: 100% !important;
802
- }}
803
- div[data-testid="column"] {{
804
- display: flex !important;
805
- flex-wrap: nowrap !important;
806
- min-width: 0 !important;
807
- flex: 1 1 auto !important;
808
- }}
809
- div[data-testid="stButton"] > button[key^="streamlit-btn-{key}-"] {{
810
- width: 20px !important;
811
- height: 20px !important;
812
- border-radius: 50% !important;
813
- background: white !important;
814
- border: none !important;
815
- color: black !important;
816
- font-size: 14px !important;
817
- padding: 0 !important;
818
- margin: 0 auto !important;
819
- box-shadow: none !important;
820
- outline: none !important;
821
- line-height: 1 !important;
822
- display: flex !important;
823
- align-items: center !important;
824
- justify-content: center !important;
825
- }}
826
- div[data-testid="stButton"] > button[key^="streamlit-btn-{key}-"]:hover {{
827
- background: #e0e0e0 !important;
828
- }}
829
- div[data-testid="stButton"] > button[key^="streamlit-btn-{key}-"]:focus,
830
- div[data-testid="stButton"] > button[key^="streamlit-btn-{key}-"]:active {{
831
- background: black !important;
832
- color: white !important;
833
- }}
834
- /* استایل دکمه انتخاب‌شده */
835
- div[data-testid="stButton"] > button[key="streamlit-btn-{key}-{st.session_state[key] if st.session_state[key] else 'none'}"] {{
836
- background: black !important;
837
- color: white !important;
838
- }}
839
- /* ریسپانسیو کردن */
840
- @media (max-width: 600px) {{
841
- .likert-line-container {{
842
- width: 90% !important;
843
- }}
844
- .likert-label-left {{
845
- left: -5% !important;
846
- font-size: 12px !important;
847
- }}
848
- .likert-label-right {{
849
- right: -5% !important;
850
- font-size: 12px !important;
851
- }}
852
- .likert-buttons {{
853
- gap: 5px !important;
854
- }}
855
- div[data-testid="stButton"] > button[key^="streamlit-btn-{key}-"] {{
856
- width: 18px !important;
857
- height: 18px !important;
858
- font-size: 12px !important;
859
- }}
860
- div[data-testid="column"] {{
861
- display: flex !important;
862
- flex-wrap: nowrap !important;
863
- min-width: 0 !important;
864
- flex: 1 1 auto !important;
865
- }}
866
  }}
867
- /* حذف استایل‌های پیش‌فرض Streamlit */
868
- div[data-testid="stButton"] > button {{
869
- border: none !important;
870
- box-shadow: none !important;
871
- outline: none !important;
872
- background: transparent !important;
873
  }}
874
- </style>
875
- """,
876
- unsafe_allow_html=True
877
- )
878
 
879
- # نمایش سوال
880
- st.markdown(f'<div class="likert-container"><div class="likert-question">{question}</div>', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
881
 
882
- # خط فرضی، لیبل‌ها، و دکمه‌ها
883
- st.markdown(
884
- f'<div class="likert-line-container">'
885
- f'<span class="likert-label-left">{labels[0]}</span>'
886
- f'<span class="likert-label-right">{labels[1]}</span>'
887
- f'<div class="likert-buttons">',
888
- unsafe_allow_html=True
889
- )
890
 
891
- # دکمه‌ها تو یه ردیف
892
- cols = st.columns(scale)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
893
  for i in range(scale):
894
- with cols[i]:
895
- value = scale - i # 7 تا 1
896
- symbol = "•" if st.session_state[key] == value else "○"
897
  if st.button(
898
- symbol,
899
- key=f"streamlit-btn-{key}-{value}",
900
- help=str(value)
901
  ):
902
  st.session_state[key] = value
903
  st.rerun()
904
-
905
- st.markdown('</div></div>', unsafe_allow_html=True)
906
-
907
- # نمایش پاسخ
908
- st.markdown(
909
- f'<div class="likert-response">پاسخ شما: {st.session_state[key] or "هیچکدام"}</div>',
910
- unsafe_allow_html=True
911
- )
912
-
913
- st.markdown('</div>', unsafe_allow_html=True)
914
-
915
  return st.session_state.get(key)
916
  def create_ride_map():
917
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
 
739
 
740
  # ========== توابع اصلی ==========
741
  def enhanced_likert_scale(question_data):
742
+ """لیکرت اسکیل با خط و نقاط - کاملاً واکنش‌گرا"""
743
  question = question_data["question"]
744
  key = question_data["key"]
745
  scale = question_data["scale"]
746
  labels = question_data.get("labels", ["کاملاً مخالفم", "کاملاً موافقم"])
747
 
 
748
  if key not in st.session_state:
749
  st.session_state[key] = None
750
+
751
+ # نمایش سوال
752
+ st.markdown(f"<div style='text-align:center; font-weight:bold; margin-bottom:15px;'>{question}</div>",
753
+ unsafe_allow_html=True)
754
+
755
+ # ایجاد خط و نقاط با HTML/CSS
756
+ scale_html = f"""
757
+ <style>
758
+ .likert-line {{
759
+ width: 80%;
760
+ height: 2px;
761
+ background: #6a0dad;
762
+ margin: 0 auto;
763
+ position: relative;
764
+ display: flex;
765
+ justify-content: space-between;
766
+ }}
767
+ .likert-dot {{
768
+ width: 20px;
769
+ height: 20px;
770
+ border-radius: 50%;
771
+ background: white;
772
+ border: 2px solid #6a0dad;
773
+ position: relative;
774
+ top: -9px;
775
+ cursor: pointer;
776
+ display: flex;
777
+ align-items: center;
778
+ justify-content: center;
779
+ }}
780
+ .likert-dot.selected {{
781
+ background: #6a0dad;
782
+ }}
783
+ .likert-labels {{
784
+ width: 80%;
785
+ margin: 5px auto 20px;
786
+ display: flex;
787
+ justify-content: space-between;
788
+ direction: rtl;
789
+ }}
790
+ .likert-value {{
791
+ text-align: center;
792
+ margin-top: 10px;
793
+ color: #6a0dad;
794
+ font-weight: bold;
795
+ }}
796
+ @media (max-width: 768px) {{
797
+ .likert-line {{
798
+ width: 90%;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
799
  }}
800
+ .likert-labels {{
801
+ width: 90%;
 
 
 
 
802
  }}
803
+ }}
804
+ </style>
 
 
805
 
806
+ <div class='likert-container'>
807
+ <div class='likert-labels'>
808
+ <span>{labels[1]}</span>
809
+ <span>{labels[0]}</span>
810
+ </div>
811
+ <div class='likert-line'>
812
+ """
813
+
814
+ # اضافه کردن نقاط
815
+ for i in range(scale):
816
+ value = scale - i
817
+ is_selected = st.session_state.get(key) == value
818
+ scale_html += f"<div class='likert-dot {'selected' if is_selected else ''}' onclick='setLikertValue({value})'></div>"
819
 
820
+ scale_html += "</div>"
 
 
 
 
 
 
 
821
 
822
+ # نمایش مقدار انتخاب شده
823
+ if st.session_state.get(key):
824
+ scale_html += f"<div class='likert-value'>پاسخ شما: {st.session_state[key]}</div>"
825
+
826
+ scale_html += "</div>"
827
+
828
+ # تزریق JavaScript برای مدیریت کلیک
829
+ components.html(scale_html + """
830
+ <script>
831
+ function setLikertValue(value) {
832
+ const streamlitDoc = window.parent.document;
833
+ const buttons = streamlitDoc.querySelectorAll('button[data-testid="baseButton-secondary"]');
834
+ buttons.forEach(btn => {
835
+ if (btn.textContent.trim() === String(value)) {
836
+ btn.click();
837
+ }
838
+ });
839
+ }
840
+ </script>
841
+ """, height=80)
842
+
843
+ # دکمه‌های واقعی (مخفی)
844
+ btn_cols = st.columns(scale)
845
  for i in range(scale):
846
+ with btn_cols[i]:
847
+ value = scale - i
 
848
  if st.button(
849
+ str(value),
850
+ key=f"{key}_btn_{value}",
851
+ type="primary" if st.session_state.get(key) == value else "secondary"
852
  ):
853
  st.session_state[key] = value
854
  st.rerun()
855
+
 
 
 
 
 
 
 
 
 
 
856
  return st.session_state.get(key)
857
  def create_ride_map():
858
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""