GSMEthesis commited on
Commit
f6004b5
·
verified ·
1 Parent(s): 69200f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -74
app.py CHANGED
@@ -772,7 +772,7 @@ body, .stApp {
772
 
773
  # ========== توابع اصلی ==========
774
  def enhanced_likert_scale(question_data):
775
- """لیکرت اسکیل با محدوده 0 تا scale و عدم نمایش نقطه برای 0"""
776
  question = question_data["question"]
777
  key = question_data["key"]
778
  scale = question_data["scale"]
@@ -786,86 +786,131 @@ def enhanced_likert_scale(question_data):
786
  st.markdown(f"<div style='text-align:center; font-weight:bold; margin-bottom:15px;'>{question}</div>",
787
  unsafe_allow_html=True)
788
 
789
- # ایجاد خط و نقاط با HTML/CSS
790
  scale_html = f"""
791
- <style>
792
- @font-face {{
793
- font-family: 'Vazir';
794
- src: url('https://cdn.fontcdn.ir/Font/Persian/Vazir/Vazir-Bold.woff2') format('woff2');
795
- }}
796
- .likert-line {{
797
- width: 80%;
798
- height: 2px;
799
- background: #6a0dad;
800
- margin: 0 auto;
801
- position: relative;
802
- display: flex;
803
- justify-content: space-between;
804
- direction: rtl;
805
- }}
806
- .likert-dot {{
807
- width: 18px;
808
- height: 18px;
809
- border-radius: 50%;
810
- background: white;
811
- border: 2px solid #6a0dad;
812
- position: relative;
813
- top: -9px;
814
- }}
815
- .likert-dot.active {{
816
- background: #6a0dad;
817
- }}
818
- .likert-labels {{
819
- width: 80%;
820
- margin: 5px auto 15px;
821
- display: flex;
822
- justify-content: space-between;
823
- direction: rtl;
824
- font-size: 14px;
825
- font-family: 'Vazir', sans-serif;
826
- font-weight: bold;
827
- color: #6a0dad;
828
- }}
829
- .separator-line {{
830
- width: 80%;
831
- height: 2px;
832
- background: #6a0dad;
833
- margin: 20px auto;
834
- }}
835
- @media (max-width: 768px) {{
836
  .likert-line {{
837
- width: 90%;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
838
  }}
839
  .likert-labels {{
840
- width: 90%;
 
 
 
 
841
  }}
842
- }}
843
- </style>
844
-
845
- <div>
846
- <div class="likert-labels">
847
- <span>{labels[1]}</span>
848
- <span>{labels[0]}</span>
 
 
 
 
 
 
 
 
 
 
849
  </div>
850
- <div class="likert-line">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
851
  """
852
-
853
- # اضافه کردن نقاط فقط برای مقادیر 1 تا scale
854
- current_value = st.session_state.get(key, 0)
855
- for i in range(1, scale+1):
856
- value = scale-i+1 # مقادیر از 1 تا scale
857
- active_class = "active" if current_value == value else ""
858
- scale_html += f"<div class='likert-dot {active_class}'></div>"
859
 
860
- scale_html += "</div></div>"
 
861
 
862
- # نمایش کامپوننت HTML
863
- components.html(scale_html, height=60)
864
-
865
- # ورودی عددی با محدوده 0 تا scale (0 قابل انتخاب است)
 
 
866
  value = st.number_input(
867
  "پاسخ شما (از 1 تا 7):",
868
- min_value=0, # 0 مجاز است
869
  max_value=scale,
870
  value=st.session_state.get(key, 0),
871
  step=1,
@@ -874,9 +919,8 @@ def enhanced_likert_scale(question_data):
874
  format="%d",
875
  on_change=lambda: st.session_state.update({key: st.session_state[f"{key}_input"]})
876
  )
877
-
878
- # خط جداکننده بنفش
879
- # خط جداکننده بنفش - تعریف استایل و HTML با هم
880
  st.markdown("""
881
  <style>
882
  .likert-separator {
@@ -890,7 +934,7 @@ def enhanced_likert_scale(question_data):
890
  """, unsafe_allow_html=True)
891
 
892
  return st.session_state.get(key)
893
-
894
  def create_ride_map():
895
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
896
  # نقاط تقریبی برای مناطق عمومی جنوب و غرب تهران
 
772
 
773
  # ========== توابع اصلی ==========
774
  def enhanced_likert_scale(question_data):
775
+ """لیکرت اسکیل تعاملی با ارتباط دوطرفه"""
776
  question = question_data["question"]
777
  key = question_data["key"]
778
  scale = question_data["scale"]
 
786
  st.markdown(f"<div style='text-align:center; font-weight:bold; margin-bottom:15px;'>{question}</div>",
787
  unsafe_allow_html=True)
788
 
789
+ # ایجاد کامپوننت تعاملی با JavaScript
790
  scale_html = f"""
791
+ <!DOCTYPE html>
792
+ <html>
793
+ <head>
794
+ <style>
795
+ @font-face {{
796
+ font-family: 'Vazir';
797
+ src: url('https://cdn.fontcdn.ir/Font/Persian/Vazir/Vazir-Bold.woff2') format('woff2');
798
+ }}
799
+ .likert-container {{
800
+ width: 80%;
801
+ margin: 0 auto;
802
+ direction: rtl;
803
+ font-family: 'Vazir', sans-serif;
804
+ }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
805
  .likert-line {{
806
+ width: 100%;
807
+ height: 2px;
808
+ background: #6a0dad;
809
+ position: relative;
810
+ margin: 20px 0;
811
+ }}
812
+ .likert-dot {{
813
+ width: 18px;
814
+ height: 18px;
815
+ border-radius: 50%;
816
+ background: white;
817
+ border: 2px solid #6a0dad;
818
+ position: absolute;
819
+ top: -9px;
820
+ cursor: pointer;
821
+ transform: translateX(-50%);
822
+ }}
823
+ .likert-dot.active {{
824
+ background: #6a0dad;
825
  }}
826
  .likert-labels {{
827
+ display: flex;
828
+ justify-content: space-between;
829
+ margin-bottom: 10px;
830
+ font-weight: bold;
831
+ color: #6a0dad;
832
  }}
833
+ .separator-line {{
834
+ width: 100%;
835
+ height: 2px;
836
+ background: #6a0dad;
837
+ margin: 20px 0;
838
+ }}
839
+ </style>
840
+ </head>
841
+ <body>
842
+ <div class="likert-container">
843
+ <div class="likert-labels">
844
+ <span>{labels[1]}</span>
845
+ <span>{labels[0]}</span>
846
+ </div>
847
+ <div class="likert-line" id="likert-line">
848
+ <!-- نقاط به صورت پویا اضافه خواهند شد -->
849
+ </div>
850
  </div>
851
+
852
+ <script>
853
+ // ایجاد نقاط
854
+ const line = document.getElementById('likert-line');
855
+ const scale = {scale};
856
+ const key = '{key}';
857
+
858
+ // پاک کردن نقاط موجود
859
+ line.innerHTML = '';
860
+
861
+ // ایجاد نقاط جدید
862
+ for (let i = 1; i <= scale; i++) {{
863
+ const dot = document.createElement('div');
864
+ dot.className = 'likert-dot';
865
+ dot.dataset.value = i;
866
+
867
+ // محاسبه موقعیت نقطه (از راست به چپ)
868
+ const position = 100 - ((i - 1) / (scale - 1)) * 100;
869
+ dot.style.left = `${{position}}%`;
870
+
871
+ // رویداد کل��ک
872
+ dot.addEventListener('click', () => {{
873
+ // حذف کلاس active از همه نقاط
874
+ document.querySelectorAll('.likert-dot').forEach(d => {{
875
+ d.classList.remove('active');
876
+ }});
877
+
878
+ // اضافه کردن کلاس active به نقطه انتخاب شده
879
+ dot.classList.add('active');
880
+
881
+ // ارسال مقدار به Streamlit
882
+ parent.window.streamlitBridge.setComponentValue({{
883
+ key: key,
884
+ value: i
885
+ }});
886
+ }});
887
+
888
+ line.appendChild(dot);
889
+ }}
890
+
891
+ // علامت گذاری نقطه انتخاب شده (اگر وجود دارد)
892
+ const currentValue = {st.session_state.get(key, 0)};
893
+ if (currentValue > 0) {{
894
+ const dot = document.querySelector(`.likert-dot[data-value="${{currentValue}}"]`);
895
+ if (dot) dot.classList.add('active');
896
+ }}
897
+ </script>
898
+ </body>
899
+ </html>
900
  """
 
 
 
 
 
 
 
901
 
902
+ # نمایش کامپوننت و دریافت مقدار
903
+ value = components.html(scale_html, height=120, key=f"{key}_component")
904
 
905
+ # به روز رسانی مقدار در session_state اگر مقدار جدید دریافت شده باشد
906
+ if value and isinstance(value, dict) and value.get("key") == key:
907
+ st.session_state[key] = value["value"]
908
+ st.experimental_rerun()
909
+
910
+ # نمایش ورودی عددی برای ویرایش دستی
911
  value = st.number_input(
912
  "پاسخ شما (از 1 تا 7):",
913
+ min_value=0,
914
  max_value=scale,
915
  value=st.session_state.get(key, 0),
916
  step=1,
 
919
  format="%d",
920
  on_change=lambda: st.session_state.update({key: st.session_state[f"{key}_input"]})
921
  )
922
+
923
+ # خط جداکننده
 
924
  st.markdown("""
925
  <style>
926
  .likert-separator {
 
934
  """, unsafe_allow_html=True)
935
 
936
  return st.session_state.get(key)
937
+
938
  def create_ride_map():
939
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
940
  # نقاط تقریبی برای مناطق عمومی جنوب و غرب تهران