GSMEthesis commited on
Commit
f3b523e
·
verified ·
1 Parent(s): 220d9e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -36
app.py CHANGED
@@ -772,7 +772,6 @@ body, .stApp {
772
  """, unsafe_allow_html=True)
773
 
774
  # ========== توابع اصلی ==========
775
-
776
  def enhanced_likert_scale(question_data):
777
  """لیکرت اسکیل با دکمه‌های رادیویی افقی و مرکزی در Streamlit"""
778
  question = question_data["question"]
@@ -804,14 +803,40 @@ def enhanced_likert_scale(question_data):
804
 
805
  for i in range(scale):
806
  with cols[i + 2]: # شروع از ستون دوم (پس از برچسب‌ها)
807
- if st.button(options[i], key=f"{key}_btn_{i}", disabled=selected_value != 0 and selected_value != int(options[i])):
808
- st.session_state[key] = int(options[i])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
809
 
810
  # نمایش برچسب سمت چپ
811
  with cols[1]:
812
  st.markdown(f"<div style='text-align:left; font-size:14px; font-weight:bold; color:#6a0dad; direction: rtl;'>{labels[1]}</div>",
813
  unsafe_allow_html=True)
814
 
 
 
 
 
 
815
  # نمایش پاسخ انتخاب‌شده
816
  if st.session_state[key] != 0:
817
  st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: {st.session_state[key]}</p>",
@@ -820,7 +845,7 @@ def enhanced_likert_scale(question_data):
820
  st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: هنوز انتخاب نشده</p>",
821
  unsafe_allow_html=True)
822
 
823
- # خط جداکننده بنفش و تنظیم چیدمان
824
  st.markdown("""
825
  <style>
826
  .likert-separator {
@@ -830,25 +855,31 @@ def enhanced_likert_scale(question_data):
830
  margin: 20px auto;
831
  direction: rtl;
832
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
833
  /* اجبار به چیدمان افقی در موبایل */
834
  @media (max-width: 768px) {
835
  .likert-separator {
836
  width: 90%;
837
  }
838
- [data-testid="stHorizontalBlock"] {
839
- display: flex !important;
840
- flex-direction: row !important;
841
- justify-content: center !important;
842
- align-items: center !important;
843
- }
844
- button {
845
  margin: 0 2px !important;
846
- width: 30px !important;
847
- height: 30px !important;
848
- border-radius: 50% !important;
849
- padding: 0 !important;
850
- text-align: center !important;
851
- line-height: 30px !important;
852
  }
853
  }
854
  /* تنظیم مرکزیت در فریم موبایل (475px) */
@@ -858,31 +889,12 @@ def enhanced_likert_scale(question_data):
858
  width: 475px !important;
859
  margin: 0 auto !important;
860
  }
861
- button {
862
- margin: 0 5px !important;
863
- width: 30px !important;
864
- height: 30px !important;
865
- border-radius: 50% !important;
866
- padding: 0 !important;
867
- text-align: center !important;
868
- line-height: 30px !important;
869
- }
870
- }
871
- button {
872
- background-color: #fff !important;
873
- border: 2px solid #6a0dad !important;
874
- color: #6a0dad !important;
875
- }
876
- button:disabled {
877
- background-color: #6a0dad !important;
878
- color: #fff !important;
879
  }
880
  </style>
881
  <div class="likert-separator"></div>
882
  """, unsafe_allow_html=True)
883
 
884
  return st.session_state.get(key)
885
-
886
  def create_ride_map():
887
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
888
  # نقاط تقریبی برای مناطق عمومی جنوب و غرب تهران
 
772
  """, unsafe_allow_html=True)
773
 
774
  # ========== توابع اصلی ==========
 
775
  def enhanced_likert_scale(question_data):
776
  """لیکرت اسکیل با دکمه‌های رادیویی افقی و مرکزی در Streamlit"""
777
  question = question_data["question"]
 
803
 
804
  for i in range(scale):
805
  with cols[i + 2]: # شروع از ستون دوم (پس از برچسب‌ها)
806
+ button_html = f"""
807
+ <button class="likert-button" data-value="{options[i]}"
808
+ style="margin: 0 5px; width: 30px; height: 30px; border-radius: 50%; padding: 0;
809
+ text-align: center; line-height: 30px; background-color: #fff;
810
+ border: 2px solid #6a0dad; color: #6a0dad;"
811
+ onclick="parent.window.parent.postMessage({{type: 'button_click', value: {options[i]}}}, '*')">
812
+ {options[i]}
813
+ </button>
814
+ <script>
815
+ window.addEventListener('message', function(event) {{
816
+ if (event.data.type === 'button_click') {{
817
+ parent.window.parent.postMessage({{type: 'update_session', key: '{key}', value: event.data.value}}, '*');
818
+ }}
819
+ }});
820
+ // بررسی اولیه برای مقدار فعلی
821
+ if ({selected_value} === {int(options[i])}) {{
822
+ document.querySelector('.likert-button[data-value="{options[i]}"]').disabled = false;
823
+ document.querySelector('.likert-button[data-value="{options[i]}"]').style.backgroundColor = '#6a0dad';
824
+ document.querySelector('.likert-button[data-value="{options[i]}"]').style.color = '#fff';
825
+ }}
826
+ </script>
827
+ """
828
+ st.components.v1.html(button_html, height=40)
829
 
830
  # نمایش برچسب سمت چپ
831
  with cols[1]:
832
  st.markdown(f"<div style='text-align:left; font-size:14px; font-weight:bold; color:#6a0dad; direction: rtl;'>{labels[1]}</div>",
833
  unsafe_allow_html=True)
834
 
835
+ # به‌روزرسانی session_state بر اساس پیام
836
+ if "button_click_value" in st.session_state and st.session_state["button_click_value"] is not None:
837
+ st.session_state[key] = st.session_state["button_click_value"]
838
+ del st.session_state["button_click_value"]
839
+
840
  # نمایش پاسخ انتخاب‌شده
841
  if st.session_state[key] != 0:
842
  st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: {st.session_state[key]}</p>",
 
845
  st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: هنوز انتخاب نشده</p>",
846
  unsafe_allow_html=True)
847
 
848
+ # خط جداکننده بنفش و استایل‌های سفارشی
849
  st.markdown("""
850
  <style>
851
  .likert-separator {
 
855
  margin: 20px auto;
856
  direction: rtl;
857
  }
858
+ /* استایل خاص برای دکمه‌های لیکرت */
859
+ .likert-button {
860
+ margin: 0 5px !important;
861
+ width: 30px !important;
862
+ height: 30px !important;
863
+ border-radius: 50% !important;
864
+ padding: 0 !important;
865
+ text-align: center !important;
866
+ line-height: 30px !important;
867
+ background-color: #fff !important;
868
+ border: 2px solid #6a0dad !important;
869
+ color: #6a0dad !important;
870
+ cursor: pointer !important;
871
+ }
872
+ .likert-button:disabled {
873
+ background-color: #6a0dad !important;
874
+ color: #fff !important;
875
+ }
876
  /* اجبار به چیدمان افقی در موبایل */
877
  @media (max-width: 768px) {
878
  .likert-separator {
879
  width: 90%;
880
  }
881
+ [data-testid="stHorizontalBlock"] .likert-button {
 
 
 
 
 
 
882
  margin: 0 2px !important;
 
 
 
 
 
 
883
  }
884
  }
885
  /* تنظیم مرکزیت در فریم موبایل (475px) */
 
889
  width: 475px !important;
890
  margin: 0 auto !important;
891
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
892
  }
893
  </style>
894
  <div class="likert-separator"></div>
895
  """, unsafe_allow_html=True)
896
 
897
  return st.session_state.get(key)
 
898
  def create_ride_map():
899
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
900
  # نقاط تقریبی برای مناطق عمومی جنوب و غرب تهران