GSMEthesis commited on
Commit
76db210
·
verified ·
1 Parent(s): d7c39f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -18
app.py CHANGED
@@ -739,7 +739,7 @@ h3 {
739
 
740
  # ========== توابع اصلی ==========
741
  def custom_likert_slider(question_data):
742
- """نمایش سوال لیکرت با اسلایدر نقطه‌ای و لیبل‌های سفارشی"""
743
  question = question_data["question"]
744
  key = question_data["key"]
745
  points = question_data["scale"]
@@ -749,7 +749,7 @@ def custom_likert_slider(question_data):
749
  default_value = (points + 1) // 2
750
  current_value = st.session_state.get(key, default_value)
751
 
752
- # HTML و JavaScript برای اسلایدر با استفاده از str.format
753
  html = """
754
  <div id="container_{0}" style="direction: ltr; font-family: 'B Nazanin'; margin-bottom: 30px;">
755
  <label style="font-size: 16px; font-weight: bold; display: block; text-align: right;">{1}</label>
@@ -764,6 +764,8 @@ def custom_likert_slider(question_data):
764
  <div style="text-align: center; margin-top: 10px; direction: rtl;">
765
  پاسخ انتخاب‌شده: <strong><span id="output_{0}">{5}</span></strong>
766
  </div>
 
 
767
  </div>
768
 
769
  <script>
@@ -771,17 +773,19 @@ def custom_likert_slider(question_data):
771
  const slider = document.getElementById(key);
772
  const value = parseInt(slider.value);
773
  document.getElementById('output_' + key).innerText = value;
774
-
775
- window.sliderValues = window.sliderValues || {{}};
776
- window.sliderValues[key] = value;
777
-
778
- try {{
779
- window.parent.postMessage({{type: 'streamlit:setComponentValue', key: "{0}", value: value}}, '*');
780
- }} catch (e) {{
781
- console.error('Error sending message to Streamlit:', e);
782
- }}
783
-
784
- localStorage.setItem(key, value);
 
 
785
  }}
786
 
787
  window.addEventListener('load', () => {{
@@ -790,24 +794,28 @@ def custom_likert_slider(question_data):
790
  if (savedValue) {{
791
  document.getElementById(key).value = savedValue;
792
  document.getElementById('output_' + key).innerText = savedValue;
793
- window.parent.postMessage({{type: 'streamlit:setComponentValue', key: "{0}", value: parseInt(savedValue)}}, '*');
794
  }}
795
  }});
796
  </script>
797
  """.format(key, question, labels[0], labels[1], points, current_value)
798
 
799
  # رندر کامپوننت HTML
800
- components.html(html, height=150)
801
 
802
- # خواندن مقدار از session_state
803
- if key in st.session_state:
804
- current_value = st.session_state[key]
 
805
  else:
806
  current_value = st.session_state.get(key, default_value)
807
 
808
  # برای دیباگ: نمایش مقدار فعلی
809
  st.write(f"مقدار فعلی برای {key}: {current_value}")
810
 
 
 
 
 
811
  return current_value
812
 
813
  def create_ride_map():
 
739
 
740
  # ========== توابع اصلی ==========
741
  def custom_likert_slider(question_data):
742
+ """نمایش سوال لیکرت با اسلایدر نقطه‌ای و لیبل‌های سفارشی با دکمه تأیید"""
743
  question = question_data["question"]
744
  key = question_data["key"]
745
  points = question_data["scale"]
 
749
  default_value = (points + 1) // 2
750
  current_value = st.session_state.get(key, default_value)
751
 
752
+ # HTML و JavaScript برای اسلایدر با دکمه تأیید
753
  html = """
754
  <div id="container_{0}" style="direction: ltr; font-family: 'B Nazanin'; margin-bottom: 30px;">
755
  <label style="font-size: 16px; font-weight: bold; display: block; text-align: right;">{1}</label>
 
764
  <div style="text-align: center; margin-top: 10px; direction: rtl;">
765
  پاسخ انتخاب‌شده: <strong><span id="output_{0}">{5}</span></strong>
766
  </div>
767
+ <button id="confirm_{0}" style="background-color: #6a0dad; color: white; border: none; border-radius: 8px; padding: 8px 16px; margin-top: 10px; cursor: pointer;"
768
+ onclick="confirmValue('{0}')">تأیید</button>
769
  </div>
770
 
771
  <script>
 
773
  const slider = document.getElementById(key);
774
  const value = parseInt(slider.value);
775
  document.getElementById('output_' + key).innerText = value;
776
+ localStorage.setItem(key, value); // ذخیره موقت در localStorage
777
+ }}
778
+
779
+ function confirmValue(key) {{
780
+ const value = parseInt(document.getElementById(key).value);
781
+ // ارسال مقدار به Streamlit با یک متغیر مخفی
782
+ const hiddenInput = document.createElement('input');
783
+ hiddenInput.type = 'hidden';
784
+ hiddenInput.name = 'slider_value_' + key;
785
+ hiddenInput.value = value;
786
+ document.getElementById('container_' + key).appendChild(hiddenInput);
787
+ // کلیک کردن روی یک دکمه مخفی در Streamlit برای دریافت مقدار
788
+ window.parent.document.querySelector('[data-testid="stButton"] > button').click();
789
  }}
790
 
791
  window.addEventListener('load', () => {{
 
794
  if (savedValue) {{
795
  document.getElementById(key).value = savedValue;
796
  document.getElementById('output_' + key).innerText = savedValue;
 
797
  }}
798
  }});
799
  </script>
800
  """.format(key, question, labels[0], labels[1], points, current_value)
801
 
802
  # رندر کامپوننت HTML
803
+ components.html(html, height=200) # افزایش ارتفاع برای نمایش دکمه
804
 
805
+ # دریافت مقدار از فرم یا session_state
806
+ if st.session_state.get(f"slider_value_{key}", None) is not None:
807
+ current_value = st.session_state[f"slider_value_{key}"]
808
+ st.session_state[key] = current_value # ذخیره در session_state با کلید اصلی
809
  else:
810
  current_value = st.session_state.get(key, default_value)
811
 
812
  # برای دیباگ: نمایش مقدار فعلی
813
  st.write(f"مقدار فعلی برای {key}: {current_value}")
814
 
815
+ # دکمه مخفی برای دریافت مقدار (فعال می‌شه با کلیک روی دکمه تأیید)
816
+ if st.button("دریافت مقدار", key=f"hidden_btn_{key}", type="primary", help="این دکمه توسط JavaScript فعال می‌شه"):
817
+ pass # این فقط برای دریافت مقدار از hidden input استفاده می‌شه
818
+
819
  return current_value
820
 
821
  def create_ride_map():