GSMEthesis commited on
Commit
b3e23d7
·
verified ·
1 Parent(s): fb5e294

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -25
app.py CHANGED
@@ -773,20 +773,19 @@ body, .stApp {
773
  """, unsafe_allow_html=True)
774
 
775
  # ========== توابع اصلی ==========
 
 
 
776
  def enhanced_likert_scale(question_data):
777
- """لیکرت اسکیل با محدوده 0 تا scale و استفاده از Flask برای مدیریت انتخاب‌ها"""
778
  question = question_data["question"]
779
  key = question_data["key"]
780
  scale = question_data["scale"]
781
  labels = question_data.get("labels", ["کاملاً مخالفم", "کاملاً موافقم"])
782
 
783
- # دریافت مقدار فعلی از سرور Flask
784
- try:
785
- response = requests.get(f"http://localhost:5000/get_selection/{key}")
786
- current_value = response.json().get('value', 0)
787
- except Exception as e:
788
- st.error(f"خطا در ارتباط با سرور: {e}")
789
- current_value = 0
790
 
791
  # نمایش سوال
792
  st.markdown(f"<div style='text-align:right; font-weight:bold; margin-bottom:15px; direction: rtl;'>{question}</div>",
@@ -818,6 +817,7 @@ def enhanced_likert_scale(question_data):
818
  position: relative;
819
  top: -9px;
820
  cursor: pointer;
 
821
  }}
822
  .likert-dot.active {{
823
  background: #6a0dad;
@@ -863,7 +863,8 @@ def enhanced_likert_scale(question_data):
863
  <div class="likert-line">
864
  """
865
 
866
- # اضافه کردن نقاط فقط برای مقادیر 1 تا scale
 
867
  for i in range(1, scale + 1):
868
  value = i
869
  active_class = "active" if current_value == value else ""
@@ -879,28 +880,31 @@ def enhanced_likert_scale(question_data):
879
  <script>
880
  window.addEventListener('message', function(event) {
881
  if (event.data.type === 'update_selection') {
882
- fetch('http://localhost:5000/update_selection', {
883
- method: 'POST',
884
- headers: { 'Content-Type': 'application/json' },
885
- body: JSON.stringify({ key: event.data.key, value: event.data.value })
886
- }).then(response => response.json()).then(data => {
887
- if (data.status === 'success') {
888
- document.querySelectorAll('.likert-dot').forEach(dot => dot.classList.remove('active'));
889
- let dots = document.querySelectorAll('.likert-dot');
890
- dots[event.data.value - 1].classList.add('active');
891
- }
892
- });
893
  }
894
  });
895
  </script>
896
  """
897
 
898
- # نمایش کامپوننت HTML
899
- components.html(scale_html, height=60)
 
 
 
 
 
900
 
901
  # نمایش پاسخ انتخاب‌شده
902
- if current_value != 0:
903
- st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: {current_value}</p>",
904
  unsafe_allow_html=True)
905
  else:
906
  st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: هنوز انتخاب نشده</p>",
@@ -925,7 +929,7 @@ def enhanced_likert_scale(question_data):
925
  <div class="likert-separator"></div>
926
  """, unsafe_allow_html=True)
927
 
928
- return current_value
929
 
930
  def create_ride_map():
931
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
 
773
  """, unsafe_allow_html=True)
774
 
775
  # ========== توابع اصلی ==========
776
+ import streamlit as st
777
+ import streamlit.components.v1 as components
778
+
779
  def enhanced_likert_scale(question_data):
780
+ """لیکرت اسکیل با HTML/CSS و مدیریت انتخاب با st.session_state"""
781
  question = question_data["question"]
782
  key = question_data["key"]
783
  scale = question_data["scale"]
784
  labels = question_data.get("labels", ["کاملاً مخالفم", "کاملاً موافقم"])
785
 
786
+ # مقداردهی اولیه با st.session_state
787
+ if key not in st.session_state:
788
+ st.session_state[key] = 0
 
 
 
 
789
 
790
  # نمایش سوال
791
  st.markdown(f"<div style='text-align:right; font-weight:bold; margin-bottom:15px; direction: rtl;'>{question}</div>",
 
817
  position: relative;
818
  top: -9px;
819
  cursor: pointer;
820
+ transition: background-color 0.2s;
821
  }}
822
  .likert-dot.active {{
823
  background: #6a0dad;
 
863
  <div class="likert-line">
864
  """
865
 
866
+ # اضافه کردن نقاط با جاوااسکریپت برای مدیریت انتخاب
867
+ current_value = st.session_state.get(key, 0)
868
  for i in range(1, scale + 1):
869
  value = i
870
  active_class = "active" if current_value == value else ""
 
880
  <script>
881
  window.addEventListener('message', function(event) {
882
  if (event.data.type === 'update_selection') {
883
+ // به‌روزرسانی st.session_state از طریق Streamlit
884
+ parent.window.parent.postMessage({
885
+ type: 'streamlit_set_component_value',
886
+ value: event.data.value
887
+ }, '*');
888
+ // به‌روزرسانی ظاهر نقاط
889
+ document.querySelectorAll('.likert-dot').forEach(dot => dot.classList.remove('active'));
890
+ let dots = document.querySelectorAll('.likert-dot');
891
+ dots[event.data.value - 1].classList.add('active');
 
 
892
  }
893
  });
894
  </script>
895
  """
896
 
897
+ # نمایش کامپوننت HTML و دریافت مقدار انتخاب‌شده
898
+ components.html(scale_html, height=60, width=600)
899
+
900
+ # به‌روزرسانی st.session_state با مقدار برگشتی
901
+ if 'streamlit_component_value' in st.session_state and st.session_state['streamlit_component_value'] is not None:
902
+ st.session_state[key] = st.session_state['streamlit_component_value']
903
+ del st.session_state['streamlit_component_value']
904
 
905
  # نمایش پاسخ انتخاب‌شده
906
+ if st.session_state[key] != 0:
907
+ st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: {st.session_state[key]}</p>",
908
  unsafe_allow_html=True)
909
  else:
910
  st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: هنوز انتخاب نشده</p>",
 
929
  <div class="likert-separator"></div>
930
  """, unsafe_allow_html=True)
931
 
932
+ return st.session_state.get(key)
933
 
934
  def create_ride_map():
935
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""