GSMEthesis commited on
Commit
99a2ef2
·
verified ·
1 Parent(s): 2d68c07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -117
app.py CHANGED
@@ -772,7 +772,7 @@ body, .stApp {
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,131 +786,86 @@ 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
- # ایجاد یک کلید منحصر به فرد برای کامپوننت
790
- component_key = f"{key}_component"
791
-
792
- # ایجاد کامپوننت تعاملی با JavaScript
793
  scale_html = f"""
794
- <!DOCTYPE html>
795
- <html>
796
- <head>
797
- <style>
798
- @font-face {{
799
- font-family: 'Vazir';
800
- src: url('https://cdn.fontcdn.ir/Font/Persian/Vazir/Vazir-Bold.woff2') format('woff2');
801
- }}
802
- .likert-container {{
803
- width: 80%;
804
- margin: 0 auto;
805
- direction: rtl;
806
- font-family: 'Vazir', sans-serif;
807
- }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808
  .likert-line {{
809
- width: 100%;
810
- height: 2px;
811
- background: #6a0dad;
812
- position: relative;
813
- margin: 20px 0;
814
- }}
815
- .likert-dot {{
816
- width: 18px;
817
- height: 18px;
818
- border-radius: 50%;
819
- background: white;
820
- border: 2px solid #6a0dad;
821
- position: absolute;
822
- top: -9px;
823
- cursor: pointer;
824
- transform: translateX(-50%);
825
- }}
826
- .likert-dot.active {{
827
- background: #6a0dad;
828
  }}
829
  .likert-labels {{
830
- display: flex;
831
- justify-content: space-between;
832
- margin-bottom: 10px;
833
- font-weight: bold;
834
- color: #6a0dad;
835
  }}
836
- </style>
837
- </head>
838
- <body>
839
- <div class="likert-container">
840
- <div class="likert-labels">
841
- <span>{labels[1]}</span>
842
- <span>{labels[0]}</span>
843
- </div>
844
- <div class="likert-line" id="likert-line">
845
- <!-- نقاط به صورت پویا اضافه خواهند شد -->
846
- </div>
847
  </div>
848
-
849
- <script>
850
- // ایجاد نقاط
851
- const line = document.getElementById('likert-line');
852
- const scale = {scale};
853
- const key = '{key}';
854
-
855
- // پاک کردن نقاط موجود
856
- line.innerHTML = '';
857
-
858
- // ایجاد نقاط جدید
859
- for (let i = 1; i <= scale; i++) {{
860
- const dot = document.createElement('div');
861
- dot.className = 'likert-dot';
862
- dot.dataset.value = i;
863
-
864
- // محاسبه موقعیت نقطه (از راست به چپ)
865
- const position = 100 - ((i - 1) / (scale - 1)) * 100;
866
- dot.style.left = `${{position}}%`;
867
-
868
- // رویداد کلیک
869
- dot.addEventListener('click', () => {{
870
- // حذف کلاس active از همه نقاط
871
- document.querySelectorAll('.likert-dot').forEach(d => {{
872
- d.classList.remove('active');
873
- }});
874
-
875
- // اضافه کردن کلاس active به نقطه انتخاب شده
876
- dot.classList.add('active');
877
-
878
- // ارسال مقدار به Streamlit
879
- window.parent.postMessage({{
880
- isStreamlitMessage: true,
881
- type: 'streamlit:setComponentValue',
882
- api: "1.0",
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)
904
-
905
- # گوش دادن به پیام‌های از کامپوننت
906
- if 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,8 +874,9 @@ def enhanced_likert_scale(question_data):
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,6 +890,7 @@ def enhanced_likert_scale(question_data):
934
  """, unsafe_allow_html=True)
935
 
936
  return st.session_state.get(key)
 
937
 
938
  def create_ride_map():
939
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
 
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
  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
  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
  """, unsafe_allow_html=True)
891
 
892
  return st.session_state.get(key)
893
+
894
 
895
  def create_ride_map():
896
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""