GSMEthesis commited on
Commit
d8ebfe1
·
verified ·
1 Parent(s): a9949ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -62
app.py CHANGED
@@ -774,7 +774,7 @@ body, .stApp {
774
 
775
  # ========== توابع اصلی ==========
776
  def enhanced_likert_scale(question_data):
777
- """لیکرت اسکیل با دکمه‌های سفارشی و مدیریت انتخاب با st.button"""
778
  question = question_data["question"]
779
  key = question_data["key"]
780
  scale = question_data["scale"]
@@ -788,42 +788,34 @@ def enhanced_likert_scale(question_data):
788
  st.markdown(f"<div style='text-align:right; font-weight:bold; margin-bottom:15px; direction: rtl;'>{question}</div>",
789
  unsafe_allow_html=True)
790
 
791
- # تنظیم چیدمان مرکزی با ستون‌ها
792
- total_cols = scale + 2 # یک ستون برای هر نقطه + 2 ستون برای برچسب‌ها
793
- col_widths = [1] * 2 + [2] * scale # 2 ستون برای برچسب‌ها (هر کدام 1) و نقاط (هر کدام 2)
794
- cols = st.columns(col_widths)
795
-
796
- # نمایش برچسب سمت راست
797
- with cols[0]:
798
- st.markdown(f"<div style='text-align:right; font-size:14px; font-weight:bold; color:#6a0dad; direction: rtl;'>{labels[0]}</div>",
799
- unsafe_allow_html=True)
 
800
 
801
- # نمایش دکمه‌های سفارشی
802
  options = list(range(1, scale + 1))
803
- selected_value = st.session_state.get(key, 0)
804
 
805
- for i, value in enumerate(options):
806
- with cols[i + 2]: # شروع از ستون دوم (پس از برچسب‌ها)
807
- button_key = f"{key}_dot_{value}"
808
- is_selected = selected_value == value
809
- button_style = "background: #6a0dad; border: 2px solid #6a0dad;" if is_selected else "background: white; border: 2px solid #6a0dad;"
810
- st.markdown(
811
- f"""
812
- <div style='text-align: center;'>
813
- <button style='width: 18px; height: 18px; border-radius: 50%; {button_style} padding: 0; cursor: pointer; border: none;'
814
- id='{button_key}' onclick='this.blur();'>
815
- </button>
816
- </div>
817
- """,
818
- unsafe_allow_html=True
819
- )
820
- if st.button("", key=button_key, help=f"Select value {value}", disabled=False):
821
- st.session_state[key] = value
822
-
823
- # نمایش برچسب سمت چپ
824
- with cols[1]:
825
- st.markdown(f"<div style='text-align:left; font-size:14px; font-weight:bold; color:#6a0dad; direction: rtl;'>{labels[1]}</div>",
826
- unsafe_allow_html=True)
827
 
828
  # نمایش پاسخ انتخاب‌شده
829
  if st.session_state[key] != 0:
@@ -833,37 +825,58 @@ def enhanced_likert_scale(question_data):
833
  st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: هنوز انتخاب نشده</p>",
834
  unsafe_allow_html=True)
835
 
836
- # خط جداکننده بنفش و استایل‌های سفارشی
837
- st.markdown("""
838
- <style>
839
- .likert-separator {
840
- width: 80%;
841
- height: 2px;
842
- background: #6a0dad;
843
- margin: 20px auto;
844
- direction: rtl;
845
- }
846
- /* اجبار به چیدمان افقی در موبایل */
847
- @media (max-width: 768px) {
848
  .likert-separator {
849
- width: 90%;
 
 
 
 
850
  }
851
- }
852
- /* تنظیم مرکزیت در فریم موبایل (475px) */
853
- @media (min-width: 768px) {
854
- [data-testid="stAppViewContainer"] [data-testid="stHorizontalBlock"] {
855
- justify-content: center !important;
856
- width: 475px !important;
857
- margin: 0 auto !important;
 
 
 
858
  }
859
- }
860
- /* مخفی کردن دکمه‌های پیش‌فرض Streamlit برای انتخاب */
861
- [data-testid="stButton"][help] {
862
- display: none !important;
863
- }
864
- </style>
865
- <div class="likert-separator"></div>
866
- """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
867
 
868
  return st.session_state.get(key)
869
 
 
774
 
775
  # ========== توابع اصلی ==========
776
  def enhanced_likert_scale(question_data):
777
+ """لیکرت اسکیل با st.radio و استایل ساده"""
778
  question = question_data["question"]
779
  key = question_data["key"]
780
  scale = question_data["scale"]
 
788
  st.markdown(f"<div style='text-align:right; font-weight:bold; margin-bottom:15px; direction: rtl;'>{question}</div>",
789
  unsafe_allow_html=True)
790
 
791
+ # نمایش برچسب‌ها
792
+ st.markdown(
793
+ f"""
794
+ <div style='display: flex; justify-content: space-between; max-width: 475px; margin: 0 auto; direction: rtl;'>
795
+ <span style='font-size: 14px; font-weight: bold; color: #6a0dad;'>{labels[0]}</span>
796
+ <span style='font-size: 14px; font-weight: bold; color: #6a0dad;'>{labels[1]}</span>
797
+ </div>
798
+ """,
799
+ unsafe_allow_html=True
800
+ )
801
 
802
+ # تنظیم چیدمان با st.radio
803
  options = list(range(1, scale + 1))
804
+ current_value = st.session_state.get(key, 0)
805
 
806
+ # استفاده از st.radio
807
+ selected_value = st.radio(
808
+ "",
809
+ options=options,
810
+ index=current_value - 1 if current_value > 0 else None,
811
+ label_visibility="collapsed",
812
+ horizontal=True,
813
+ key=f"{key}_radio"
814
+ )
815
+
816
+ # به‌روزرسانی st.session_state
817
+ if selected_value is not None and selected_value != current_value:
818
+ st.session_state[key] = selected_value
 
 
 
 
 
 
 
 
 
819
 
820
  # نمایش پاسخ انتخاب‌شده
821
  if st.session_state[key] != 0:
 
825
  st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: هنوز انتخاب نشده</p>",
826
  unsafe_allow_html=True)
827
 
828
+ # خط جداکننده و استایل سفارشی
829
+ st.markdown(
830
+ """
831
+ <style>
 
 
 
 
 
 
 
 
832
  .likert-separator {
833
+ width: 80%;
834
+ height: 2px;
835
+ background: #6a0dad;
836
+ margin: 20px auto;
837
+ direction: rtl;
838
  }
839
+ /* تنظیم استایل دکمه‌های رادیویی */
840
+ [data-testid="stRadio"] > div {
841
+ display: flex;
842
+ justify-content: space-between;
843
+ max-width: 475px;
844
+ margin: 0 auto;
845
+ padding: 0;
846
+ background: #6a0dad;
847
+ height: 2px;
848
+ align-items: center;
849
  }
850
+ [data-testid="stRadio"] input[type="radio"] {
851
+ display: none;
852
+ }
853
+ [data-testid="stRadio"] label {
854
+ width: 18px !important;
855
+ height: 18px !important;
856
+ border-radius: 50% !important;
857
+ background: white !important;
858
+ border: 2px solid #6a0dad !important;
859
+ margin: 0 !important;
860
+ position: relative;
861
+ top: -9px;
862
+ cursor: pointer;
863
+ }
864
+ [data-testid="stRadio"] input[type="radio"]:checked + label {
865
+ background: #6a0dad !important;
866
+ }
867
+ @media (max-width: 768px) {
868
+ .likert-separator {
869
+ width: 90%;
870
+ }
871
+ [data-testid="stRadio"] > div {
872
+ width: 90%;
873
+ }
874
+ }
875
+ </style>
876
+ <div class="likert-separator"></div>
877
+ """,
878
+ unsafe_allow_html=True
879
+ )
880
 
881
  return st.session_state.get(key)
882