GSMEthesis commited on
Commit
f6aa431
·
verified ·
1 Parent(s): db756d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -22
app.py CHANGED
@@ -674,31 +674,30 @@ h3 {
674
  """, unsafe_allow_html=True)
675
 
676
  # ========== توابع اصلی ==========
677
- def custom_likert_slider(question, key, points=7):
678
- # تعریف گزینه‌ها
679
- options = {
680
- 5: ["کاملاً مخالفم", "مخالفم", "خنثی", "موافقم", "کاملاً موافقم"],
681
- 7: ["کاملاً مخالفم", "نسبتاً مخالفم", "کمی مخالفم", "خنثی", "کمی موافقم", "نسبتاً موافقم", "کاملاً موافقم"]
682
- }
683
- labels = options.get(points, options[7])
684
-
685
- # بررسی و تعیین مقدار پیش‌فرض
686
- raw_value = st.session_state.get(key)
687
- default_value = raw_value if isinstance(raw_value, int) else (points + 1) // 2
688
-
689
  # HTML و JavaScript
690
  html = f"""
691
- <div id="container_{key}" style="direction: rtl; font-family: 'B Nazanin'; margin-bottom: 30px;">
692
- <label style="font-size: 16px; font-weight: bold;">{question}</label>
693
  <div style="display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 5px;">
694
- {''.join(f'<span style="flex: 1; text-align: center;">{label}</span>' for label in labels)}
 
695
  </div>
696
  <input type="range" id="{key}" min="1" max="{points}" step="1" value="{default_value}"
697
  style="width: 100%; height: 10px; accent-color: #6a0dad; margin-bottom: 15px;"
698
  oninput="updateSlider('{key}')">
699
- <div style="text-align: center; margin-top: 10px;">
700
  پاسخ انتخاب‌شده: <strong><span id="output_{key}">{default_value}</span></strong>
701
- <span id="label_{key}" style="color: #6a0dad; margin-right: 5px;">({labels[default_value-1]})</span>
702
  </div>
703
  </div>
704
 
@@ -706,7 +705,6 @@ def custom_likert_slider(question, key, points=7):
706
  function updateSlider(key) {{
707
  const value = parseInt(document.getElementById(key).value);
708
  document.getElementById('output_' + key).innerText = value;
709
- document.getElementById('label_' + key).innerText = '(' + {json.dumps(labels)}[value-1] + ')';
710
 
711
  // ارسال مقدار به Streamlit
712
  window.parent.postMessage({{
@@ -717,11 +715,11 @@ def custom_likert_slider(question, key, points=7):
717
  }}
718
  </script>
719
  """
720
-
721
  # نمایش کامپوننت
722
- components.html(html, height=180)
723
-
724
- # مقدار نهایی (از session_state)
725
  return st.session_state.get(key, default_value)
726
 
727
  def create_ride_map():
 
674
  """, unsafe_allow_html=True)
675
 
676
  # ========== توابع اصلی ==========
677
+ def custom_likert_slider(question, key, points=7, labels=None):
678
+ # تعیین لیبل‌های پیش‌فرض اگر ارائه نشده باشند
679
+ if not labels:
680
+ labels = {
681
+ 5: ["کاملاً مخالفم", "کاملاً موافقم"],
682
+ 7: ["کاملاً مخالفم", "کاملاً موافقم"]
683
+ }.get(points, ["کمترین", "بیشترین"])
684
+
685
+ # مقدار پیش‌فرض (وسط طیف)
686
+ default_value = st.session_state.get(key, (points + 1) // 2)
687
+
 
688
  # HTML و JavaScript
689
  html = f"""
690
+ <div id="container_{key}" style="direction: ltr; font-family: 'B Nazanin'; margin-bottom: 30px;">
691
+ <label style="font-size: 16px; font-weight: bold; display: block; text-align: right;">{question}</label>
692
  <div style="display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 5px;">
693
+ <span>{labels[0]}</span>
694
+ <span>{labels[1]}</span>
695
  </div>
696
  <input type="range" id="{key}" min="1" max="{points}" step="1" value="{default_value}"
697
  style="width: 100%; height: 10px; accent-color: #6a0dad; margin-bottom: 15px;"
698
  oninput="updateSlider('{key}')">
699
+ <div style="text-align: center; margin-top: 10px; direction: rtl;">
700
  پاسخ انتخاب‌شده: <strong><span id="output_{key}">{default_value}</span></strong>
 
701
  </div>
702
  </div>
703
 
 
705
  function updateSlider(key) {{
706
  const value = parseInt(document.getElementById(key).value);
707
  document.getElementById('output_' + key).innerText = value;
 
708
 
709
  // ارسال مقدار به Streamlit
710
  window.parent.postMessage({{
 
715
  }}
716
  </script>
717
  """
718
+
719
  # نمایش کامپوننت
720
+ components.html(html, height=150)
721
+
722
+ # مقدار نهایی
723
  return st.session_state.get(key, default_value)
724
 
725
  def create_ride_map():