GSMEthesis commited on
Commit
cd5ec29
·
verified ·
1 Parent(s): 0afad43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -30
app.py CHANGED
@@ -739,46 +739,74 @@ h3 {
739
 
740
  # ========== توابع اصلی ==========
741
  def custom_likert_slider(question_data):
742
- """نمایش سوال لیکرت با اسلایدر استاندارد Streamlit"""
743
  question = question_data["question"]
744
  key = question_data["key"]
745
  points = question_data["scale"]
746
  labels = question_data.get("labels", ["کمترین", "بیشترین"]) # لیبل‌های پیش‌فرض
747
-
748
  # مقدار پیش‌فرض (وسط طیف)
749
  default_value = (points + 1) // 2
750
-
751
- # نمایش سوال
752
- st.markdown(f"<p style='font-size:16px; margin-bottom:5px;'>{question}</p>", unsafe_allow_html=True)
753
-
754
- # اسلایدر استاندارد Streamlit
755
- value = st.slider(
756
- "",
757
- min_value=1,
758
- max_value=points,
759
- value=default_value,
760
- step=1,
761
- key=key,
762
- format="%d"
763
- )
764
-
765
- # نمایش لیبل‌های دو طرف
766
- st.markdown(
767
- f"""
768
- <div style='display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 5px;'>
769
  <span>{labels[0]}</span>
770
  <span>{labels[1]}</span>
771
  </div>
772
- <p style='text-align:center; color:#6a0dad; font-weight:bold;'>
773
- پاسخ انتخاب‌شده: {value}
774
- </p>
775
- """,
776
- unsafe_allow_html=True
777
- )
 
778
 
779
- # ذخیره مقدار در session_state
780
- st.session_state[key] = value
781
- return value
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
782
 
783
  def create_ride_map():
784
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
 
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"]
746
  labels = question_data.get("labels", ["کمترین", "بیشترین"]) # لیبل‌های پیش‌فرض
747
+
748
  # مقدار پیش‌فرض (وسط طیف)
749
  default_value = (points + 1) // 2
750
+
751
+ # مقدار فعلی از session_state
752
+ current_value = st.session_state.get(key, default_value)
753
+
754
+ # HTML و JavaScript برای اسلایدر
755
+ html = f"""
756
+ <div id="container_{key}" style="direction: ltr; font-family: 'B Nazanin'; margin-bottom: 30px;">
757
+ <label style="font-size: 16px; font-weight: bold; display: block; text-align: right;">{question}</label>
758
+ <div style="display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 5px;">
 
 
 
 
 
 
 
 
 
 
759
  <span>{labels[0]}</span>
760
  <span>{labels[1]}</span>
761
  </div>
762
+ <input type="range" id="{key}" min="1" max="{points}" step="1" value="{current_value}"
763
+ style="width: 100%; height: 10px; accent-color: #6a0dad; margin-bottom: 15px;"
764
+ oninput="updateSlider('{key}')" onchange="updateSlider('{key}')">
765
+ <div style="text-align: center; margin-top: 10px; direction: rtl;">
766
+ پاسخ انتخاب‌شده: <strong><span id="output_{key}">{current_value}</span></strong>
767
+ </div>
768
+ </div>
769
 
770
+ <script>
771
+ function updateSlider(key) {{
772
+ const slider = document.getElementById(key);
773
+ const value = parseInt(slider.value);
774
+ document.getElementById('output_' + key).innerText = value;
775
+
776
+ // ارسال مقدار به Streamlit
777
+ const message = {{
778
+ type: 'streamlit:setComponentValue',
779
+ value: value,
780
+ key: key
781
+ }};
782
+ window.parent.postMessage(message, '*');
783
+
784
+ // دیباگ: چاپ پیام ارسالی
785
+ console.log('Sending to Streamlit: ', JSON.stringify(message));
786
+ }}
787
+
788
+ // تنظیم مقدار اولیه
789
+ document.getElementById('{key}').value = {current_value};
790
+ document.getElementById('output_{key}').innerText = {current_value};
791
+ </script>
792
+ """
793
+
794
+ # نمایش کامپوننت با کلید منحصربه‌فرد
795
+ components.html(html, height=150, key=f"slider_{key}")
796
+
797
+ # دریافت مقدار از Streamlit
798
+ component_value = st.session_state.get(key, default_value)
799
+
800
+ # دیباگ: نمایش مقادیر
801
+ st.write(f"Debug: کلید: {key}")
802
+ st.write(f"Debug: مقدار پیش‌فرض: {default_value}")
803
+ st.write(f"Debug: مقدار دریافت‌شده: {component_value}")
804
+ st.write(f"Debug: مقدار در session_state[{key}]: {st.session_state.get(key, 'هیچ')}")
805
+
806
+ # ذخیره مقدار
807
+ st.session_state[key] = component_value
808
+
809
+ return component_value
810
 
811
  def create_ride_map():
812
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""