GSMEthesis commited on
Commit
27be689
·
verified ·
1 Parent(s): cd5ec29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -56
app.py CHANGED
@@ -734,80 +734,118 @@ h3 {
734
  margin-bottom: 1em !important;
735
  line-height: 1.8 !important;
736
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
737
  </style>
738
  """, unsafe_allow_html=True)
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 - نسخه اصلاح شده با مناطق عمومی"""
813
  # نقاط تقریبی برای مناطق عمومی جنوب و غرب تهران
 
734
  margin-bottom: 1em !important;
735
  line-height: 1.8 !important;
736
  }
737
+ /* استایل برای اسلایدر استاندارد Streamlit */
738
+ .stSlider [role="slider"] {
739
+ background-color: #f0e6ff !important; /* رنگ پس‌زمینه مسیر */
740
+ height: 10px !important;
741
+ border-radius: 5px !important;
742
+ }
743
+
744
+ .stSlider [role="slider"]::-webkit-slider-thumb {
745
+ background-color: #6a0dad !important; /* رنگ دستگیره */
746
+ border: 2px solid #ffffff !important;
747
+ width: 20px !important;
748
+ height: 20px !important;
749
+ border-radius: 50% !important;
750
+ cursor: pointer !important;
751
+ box-shadow: 0 2px 5px rgba(0,0,0,0.2) !important;
752
+ }
753
+
754
+ .stSlider [role="slider"]::-moz-range-thumb {
755
+ background-color: #6a0dad !important;
756
+ border: 2px solid #ffffff !important;
757
+ width: 20px !important;
758
+ height: 20px !important;
759
+ border-radius: 50% !important;
760
+ cursor: pointer !important;
761
+ box-shadow: 0 2px 5px rgba(0,0,0,0.2) !important;
762
+ }
763
+
764
+ .stSlider [role="slider"]::-webkit-slider-runnable-track {
765
+ background-color: #f0e6ff !important;
766
+ height: 10px !important;
767
+ border-radius: 5px !important;
768
+ }
769
+
770
+ .stSlider [role="slider"]::-moz-range-track {
771
+ background-color: #f0e6ff !important;
772
+ height: 10px !important;
773
+ border-radius: 5px !important;
774
+ }
775
+
776
+ /* استایل برای لیبل‌های اسلایدر */
777
+ .slider-labels {
778
+ display: flex !important;
779
+ justify-content: space-between !important;
780
+ font-size: 12px !important;
781
+ margin-bottom: 5px !important;
782
+ color: #333333 !important;
783
+ font-family: 'B Nazanin' !important;
784
+ }
785
+
786
+ /* استایل برای متن پاسخ انتخاب‌شده */
787
+ .slider-response {
788
+ text-align: center !important;
789
+ color: #6a0dad !important;
790
+ font-weight: bold !important;
791
+ margin-top: 10px !important;
792
+ font-family: 'B Nazanin' !important;
793
+ }
794
  </style>
795
  """, unsafe_allow_html=True)
796
 
797
  # ========== توابع اصلی ==========
798
  def custom_likert_slider(question_data):
799
+ """نمایش سوال لیکرت با اسلایدر استاندارد Streamlit و استایل سفارشی"""
800
  question = question_data["question"]
801
  key = question_data["key"]
802
  points = question_data["scale"]
803
  labels = question_data.get("labels", ["کمترین", "بیشترین"]) # لیبل‌های پیش‌فرض
804
+
805
  # مقدار پیش‌فرض (وسط طیف)
806
  default_value = (points + 1) // 2
807
+
808
+ # نمایش سوال
809
+ st.markdown(f"<p style='font-size:16px; margin-bottom:5px;'>{question}</p>", unsafe_allow_html=True)
810
+
811
+ # لیبل‌های دو طرف
812
+ st.markdown(
813
+ f"""
814
+ <div class="slider-labels">
 
815
  <span>{labels[0]}</span>
816
  <span>{labels[1]}</span>
817
  </div>
818
+ """,
819
+ unsafe_allow_html=True
820
+ )
 
 
 
 
821
 
822
+ # اسلایدر استاندارد Streamlit
823
+ value = st.slider(
824
+ "",
825
+ min_value=1,
826
+ max_value=points,
827
+ value=st.session_state.get(key, default_value),
828
+ step=1,
829
+ key=key,
830
+ format="%d"
831
+ )
 
 
 
 
 
 
 
832
 
833
+ # نمایش پاسخ انتخاب‌شده
834
+ st.markdown(
835
+ f"""
836
+ <p class="slider-response">
837
+ پاسخ انتخاب‌شده: {value}
838
+ </p>
839
+ """,
840
+ unsafe_allow_html=True
841
+ )
842
+
843
+ # دیباگ
844
+ st.write(f"Debug: مقدار ذخیره‌شده برای {key}: {value}")
 
 
 
 
 
845
 
846
  # ذخیره مقدار
847
+ st.session_state[key] = value
848
+ return value
 
 
849
  def create_ride_map():
850
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
851
  # نقاط تقریبی برای مناطق عمومی جنوب و غرب تهران