GSMEthesis commited on
Commit
a284e0e
·
verified ·
1 Parent(s): 8011e4b

Update app.py

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