GSMEthesis commited on
Commit
81c0460
·
verified ·
1 Parent(s): 99a2ef2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -83
app.py CHANGED
@@ -772,12 +772,11 @@ 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"]
779
  labels = question_data.get("labels", ["کاملاً مخالفم", "کاملاً موافقم"])
780
-
781
  # مقداردهی اولیه
782
  if key not in st.session_state:
783
  st.session_state[key] = 0
@@ -786,97 +785,92 @@ 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,
872
  key=f"{key}_input",
873
  placeholder="0 (پاسخ نداده) یا 1-7",
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 {
@@ -889,8 +883,7 @@ def enhanced_likert_scale(question_data):
889
  <div class="likert-separator"></div>
890
  """, unsafe_allow_html=True)
891
 
892
- return st.session_state.get(key)
893
-
894
 
895
  def create_ride_map():
896
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
 
772
 
773
  # ========== توابع اصلی ==========
774
  def enhanced_likert_scale(question_data):
 
775
  question = question_data["question"]
776
  key = question_data["key"]
777
  scale = question_data["scale"]
778
  labels = question_data.get("labels", ["کاملاً مخالفم", "کاملاً موافقم"])
779
+
780
  # مقداردهی اولیه
781
  if key not in st.session_state:
782
  st.session_state[key] = 0
 
785
  st.markdown(f"<div style='text-align:center; font-weight:bold; margin-bottom:15px;'>{question}</div>",
786
  unsafe_allow_html=True)
787
 
788
+ # تابع کمکی برای ساخت HTML پویا
789
+ def build_scale_html(current_value):
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: Ascending;
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 {{ width: 90%; }}
837
+ .likert-labels {{ width: 90%; }}
838
+ }}
839
+ </style>
840
+ <div>
841
+ <div class="likert-labels">
842
+ <span>{labels[1]}</span>
843
+ <span>{labels[0]}</span>
844
+ </div>
845
+ <div class="likert-line">
846
+ """
847
+ for i in range(1, scale + 1):
848
+ value = scale - i + 1
849
+ active_class = "active" if current_value == value else ""
850
+ scale_html += f"<div class='likert-dot {active_class}'></div>"
851
+ scale_html += "</div></div>"
852
+ return scale_html
853
+
854
+ # رندر HTML با مقدار فعلی
855
+ components.html(build_scale_html(st.session_state[key]), height=60)
856
+
857
+ # ورودی عددی
858
  value = st.number_input(
859
  "پاسخ شما (از 1 تا 7):",
860
+ min_value=0,
861
  max_value=scale,
862
+ value=st.session_state[key],
863
  step=1,
864
  key=f"{key}_input",
865
  placeholder="0 (پاسخ نداده) یا 1-7",
866
+ format="%d"
 
867
  )
868
 
869
+ # به‌روزرسانی مقدار در session_state
870
+ if value != st.session_state[key]:
871
+ st.session_state[key] = value
872
+
873
+ # خط جداکننده
874
  st.markdown("""
875
  <style>
876
  .likert-separator {
 
883
  <div class="likert-separator"></div>
884
  """, unsafe_allow_html=True)
885
 
886
+ return st.session_state[key]
 
887
 
888
  def create_ride_map():
889
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""