GSMEthesis commited on
Commit
5b76660
·
verified ·
1 Parent(s): dbecb1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -738,8 +738,6 @@ h3 {
738
  """, unsafe_allow_html=True)
739
 
740
  # ========== توابع اصلی ==========
741
-
742
-
743
  def custom_likert_slider(question_data):
744
  """Custom Likert-style question with circles and labels."""
745
  question = question_data["question"]
@@ -747,21 +745,22 @@ def custom_likert_slider(question_data):
747
  scale = question_data["scale"]
748
  labels = question_data.get("labels", ["کمترین", "بیشترین"]) # Default labels
749
 
750
- # Default value (middle of the scale)
751
- default_value = st.session_state.get(key, (scale + 1) // 2)
 
752
 
753
- # HTML and CSS for custom Likert scale with radio buttons (styled as circles)
754
  html = f"""
755
  <div style="direction: ltr; font-family: 'B Nazanin'; margin-bottom: 30px;">
756
  <label style="font-size: 16px; font-weight: bold; display: block; text-align: center;">{question}</label>
757
  <div style="display: flex; justify-content: center; align-items: center; gap: 10px; margin: 10px 0;">
758
  <span>{labels[0]}</span>
759
  <div style="display: flex; gap: 10px;">
760
- <input type="radio" id="radio_{key}_1" name="{key}" value="1" style="width: 30px; height: 30px;" {'checked' if default_value == 1 else ''} onclick="updateSlider(1, '{key}')">
761
- <input type="radio" id="radio_{key}_2" name="{key}" value="2" style="width: 30px; height: 30px;" {'checked' if default_value == 2 else ''} onclick="updateSlider(2, '{key}')">
762
- <input type="radio" id="radio_{key}_3" name="{key}" value="3" style="width: 30px; height: 30px;" {'checked' if default_value == 3 else ''} onclick="updateSlider(3, '{key}')">
763
- <input type="radio" id="radio_{key}_4" name="{key}" value="4" style="width: 30px; height: 30px;" {'checked' if default_value == 4 else ''} onclick="updateSlider(4, '{key}')">
764
- <input type="radio" id="radio_{key}_5" name="{key}" value="5" style="width: 30px; height: 30px;" {'checked' if default_value == 5 else ''} onclick="updateSlider(5, '{key}')">
765
  </div>
766
  <span>{labels[1]}</span>
767
  </div>
@@ -769,22 +768,34 @@ def custom_likert_slider(question_data):
769
 
770
  <script>
771
  function updateSlider(value, key) {{
772
- // Send the value back to Streamlit
773
  window.parent.postMessage({{
774
  type: 'streamlit:setComponentValue',
775
  key: key,
776
- value: value
777
  }}, '*');
778
  }}
 
 
 
 
 
 
 
 
 
779
  </script>
780
  """
781
 
782
- # Render the HTML and JavaScript in the Streamlit app
783
  components.html(html, height=200)
784
 
785
- # Return the value stored in session state, initially set to default value
786
- slider_value = st.session_state.get(key, default_value)
787
- return slider_value
 
 
 
788
 
789
  def create_ride_map():
790
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده ب�� مناطق عمومی"""
 
738
  """, unsafe_allow_html=True)
739
 
740
  # ========== توابع اصلی ==========
 
 
741
  def custom_likert_slider(question_data):
742
  """Custom Likert-style question with circles and labels."""
743
  question = question_data["question"]
 
745
  scale = question_data["scale"]
746
  labels = question_data.get("labels", ["کمترین", "بیشترین"]) # Default labels
747
 
748
+ # مقدار پیش‌فرض فقط برای بار اول (اگر هیچ مقداری در session_state وجود نداشته باشد)
749
+ if key not in st.session_state:
750
+ st.session_state[key] = (scale + 1) // 2 # مقدار میانی به عنوان پیش‌فرض
751
 
752
+ # HTML و JavaScript برای دکمه‌های رادیویی
753
  html = f"""
754
  <div style="direction: ltr; font-family: 'B Nazanin'; margin-bottom: 30px;">
755
  <label style="font-size: 16px; font-weight: bold; display: block; text-align: center;">{question}</label>
756
  <div style="display: flex; justify-content: center; align-items: center; gap: 10px; margin: 10px 0;">
757
  <span>{labels[0]}</span>
758
  <div style="display: flex; gap: 10px;">
759
+ <input type="radio" id="radio_{key}_1" name="{key}" value="1" style="width: 30px; height: 30px;" {'checked' if st.session_state[key] == 1 else ''} onclick="updateSlider(1, '{key}')">
760
+ <input type="radio" id="radio_{key}_2" name="{key}" value="2" style="width: 30px; height: 30px;" {'checked' if st.session_state[key] == 2 else ''} onclick="updateSlider(2, '{key}')">
761
+ <input type="radio" id="radio_{key}_3" name="{key}" value="3" style="width: 30px; height: 30px;" {'checked' if st.session_state[key] == 3 else ''} onclick="updateSlider(3, '{key}')">
762
+ <input type="radio" id="radio_{key}_4" name="{key}" value="4" style="width: 30px; height: 30px;" {'checked' if st.session_state[key] == 4 else ''} onclick="updateSlider(4, '{key}')">
763
+ <input type="radio" id="radio_{key}_5" name="{key}" value="5" style="width: 30px; height: 30px;" {'checked' if st.session_state[key] == 5 else ''} onclick="updateSlider(5, '{key}')">
764
  </div>
765
  <span>{labels[1]}</span>
766
  </div>
 
768
 
769
  <script>
770
  function updateSlider(value, key) {{
771
+ // ارسال مقدار به Streamlit
772
  window.parent.postMessage({{
773
  type: 'streamlit:setComponentValue',
774
  key: key,
775
+ value: parseInt(value) // تبدیل به عدد صحیح
776
  }}, '*');
777
  }}
778
+
779
+ // دریافت پیام از Streamlit برای به‌روزرسانی session_state
780
+ window.addEventListener('message', function(event) {{
781
+ if (event.data.type === 'streamlit:render') {{
782
+ // به‌روزرسانی دکمه‌های رادیویی بر اساس مقدار session_state
783
+ let value = {st.session_state[key]};
784
+ document.querySelector(`#radio_{key}_${value}`).checked = true;
785
+ }}
786
+ }});
787
  </script>
788
  """
789
 
790
+ # رندر HTML و JavaScript
791
  components.html(html, height=200)
792
 
793
+ # مدیریت پیام‌های دریافتی از JavaScript
794
+ if f"{key}_component" in st.session_state:
795
+ st.session_state[key] = st.session_state[f"{key}_component"]
796
+
797
+ # بازگرداندن مقدار ذخیره‌شده در session_state
798
+ return st.session_state[key]
799
 
800
  def create_ride_map():
801
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده ب�� مناطق عمومی"""