GSMEthesis commited on
Commit
0338218
·
verified ·
1 Parent(s): 8626f4b

Update app.py

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