GSMEthesis commited on
Commit
fb5e294
·
verified ·
1 Parent(s): 062ffc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -64
app.py CHANGED
@@ -10,6 +10,7 @@ import os
10
  import json
11
  import random
12
  import time
 
13
 
14
  # تنظیمات اولیهه
15
  st.set_page_config(layout="wide", page_title="راهیار - تحلیل انصاف قیمتی", page_icon="🚖")
@@ -773,57 +774,139 @@ body, .stApp {
773
 
774
  # ========== توابع اصلی ==========
775
  def enhanced_likert_scale(question_data):
776
- """لیکرت اسکیل با دکمه‌های رادیویی افقی و مرکزی در Streamlit"""
777
  question = question_data["question"]
778
  key = question_data["key"]
779
  scale = question_data["scale"]
780
  labels = question_data.get("labels", ["کاملاً مخالفم", "کاملاً موافقم"])
781
 
782
- # مقداردهی اولیه
783
- if key not in st.session_state:
784
- st.session_state[key] = 0
 
 
 
 
785
 
786
  # نمایش سوال
787
  st.markdown(f"<div style='text-align:right; font-weight:bold; margin-bottom:15px; direction: rtl;'>{question}</div>",
788
  unsafe_allow_html=True)
789
 
790
- # تنظیم چیدمان مرکزی با ستون‌ها
791
- total_cols = scale + 2 # یک ستون برای هر نقطه + 2 ستون برای برچسب‌ها
792
- col_widths = [1] * 2 + [2] * scale # 2 ستون برای برچسب‌ها (هر کدام 1) و نقاط (هر کدام 2)
793
- cols = st.columns(col_widths)
794
-
795
- # نمایش برچسب سمت راست
796
- with cols[0]:
797
- st.markdown(f"<div style='text-align:right; font-size:14px; font-weight:bold; color:#6a0dad; direction: rtl;'>{labels[0]}</div>",
798
- unsafe_allow_html=True)
799
-
800
- # نمایش دکمه‌های رادیویی به صورت افقی و مرکزی
801
- options = [str(i) for i in range(1, scale + 1)]
802
- selected_value = st.session_state.get(key, 0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
803
 
804
- for i in range(scale):
805
- with cols[i + 2]: # شروع از ستون دوم (پس از برچسب‌ها)
806
- button_key = f"{key}_btn_{i}"
807
- is_selected = selected_value == int(options[i])
808
- st.markdown(f'<div style="text-align: center;">', unsafe_allow_html=True)
809
- if st.button(options[i], key=button_key, disabled=selected_value != 0 and not is_selected):
810
- st.session_state[key] = int(options[i])
811
- st.markdown(f'</div>', unsafe_allow_html=True)
812
-
813
- # نمایش برچسب سمت چپ
814
- with cols[1]:
815
- st.markdown(f"<div style='text-align:left; font-size:14px; font-weight:bold; color:#6a0dad; direction: rtl;'>{labels[1]}</div>",
816
- unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
817
 
818
  # نمایش پاسخ انتخاب‌شده
819
- if st.session_state[key] != 0:
820
- st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: {st.session_state[key]}</p>",
821
  unsafe_allow_html=True)
822
  else:
823
  st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: هنوز انتخاب نشده</p>",
824
  unsafe_allow_html=True)
825
 
826
- # خط جداکننده بنفش و استایل‌های سفارشی
827
  st.markdown("""
828
  <style>
829
  .likert-separator {
@@ -833,46 +916,16 @@ def enhanced_likert_scale(question_data):
833
  margin: 20px auto;
834
  direction: rtl;
835
  }
836
- /* استایل خاص برای دکمه‌های لیکرت */
837
- [data-testid="stButton"] {
838
- margin: 0 5px !important;
839
- width: 30px !important;
840
- height: 30px !important;
841
- border-radius: 50% !important;
842
- padding: 0 !important;
843
- text-align: center !important;
844
- line-height: 30px !important;
845
- background-color: #fff !important;
846
- border: 2px solid #6a0dad !important;
847
- color: #6a0dad !important;
848
- cursor: pointer !important;
849
- }
850
- [data-testid="stButton"]:disabled {
851
- background-color: #6a0dad !important;
852
- color: #fff !important;
853
- }
854
- /* اجبار به چیدمان افقی در موبایل */
855
  @media (max-width: 768px) {
856
  .likert-separator {
857
  width: 90%;
858
  }
859
- [data-testid="stHorizontalBlock"] [data-testid="stButton"] {
860
- margin: 0 2px !important;
861
- }
862
- }
863
- /* تنظیم مرکزیت در فریم موبایل (475px) */
864
- @media (min-width: 768px) {
865
- [data-testid="stAppViewContainer"] [data-testid="stHorizontalBlock"] {
866
- justify-content: center !important;
867
- width: 475px !important;
868
- margin: 0 auto !important;
869
- }
870
  }
871
  </style>
872
  <div class="likert-separator"></div>
873
  """, unsafe_allow_html=True)
874
 
875
- return st.session_state.get(key)
876
 
877
  def create_ride_map():
878
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
 
10
  import json
11
  import random
12
  import time
13
+ import requests
14
 
15
  # تنظیمات اولیهه
16
  st.set_page_config(layout="wide", page_title="راهیار - تحلیل انصاف قیمتی", page_icon="🚖")
 
774
 
775
  # ========== توابع اصلی ==========
776
  def enhanced_likert_scale(question_data):
777
+ """لیکرت اسکیل با محدوده 0 تا scale و استفاده از Flask برای مدیریت انتخاب‌ها"""
778
  question = question_data["question"]
779
  key = question_data["key"]
780
  scale = question_data["scale"]
781
  labels = question_data.get("labels", ["کاملاً مخالفم", "کاملاً موافقم"])
782
 
783
+ # دریافت مقدار فعلی از سرور Flask
784
+ try:
785
+ response = requests.get(f"http://localhost:5000/get_selection/{key}")
786
+ current_value = response.json().get('value', 0)
787
+ except Exception as e:
788
+ st.error(f"خطا در ارتباط با سرور: {e}")
789
+ current_value = 0
790
 
791
  # نمایش سوال
792
  st.markdown(f"<div style='text-align:right; font-weight:bold; margin-bottom:15px; direction: rtl;'>{question}</div>",
793
  unsafe_allow_html=True)
794
 
795
+ # ایجاد خط و نقاط با HTML/CSS
796
+ scale_html = f"""
797
+ <style>
798
+ @font-face {{
799
+ font-family: 'B Nazanin';
800
+ src: url('https://www.fontyab.com/fonts/b-nazanin/BNazanin.woff2') format('woff2');
801
+ }}
802
+ .likert-line {{
803
+ width: 80%;
804
+ height: 2px;
805
+ background: #6a0dad;
806
+ margin: 0 auto;
807
+ position: relative;
808
+ display: flex;
809
+ justify-content: space-between;
810
+ direction: rtl;
811
+ }}
812
+ .likert-dot {{
813
+ width: 18px;
814
+ height: 18px;
815
+ border-radius: 50%;
816
+ background: white;
817
+ border: 2px solid #6a0dad;
818
+ position: relative;
819
+ top: -9px;
820
+ cursor: pointer;
821
+ }}
822
+ .likert-dot.active {{
823
+ background: #6a0dad;
824
+ }}
825
+ .likert-labels {{
826
+ width: 80%;
827
+ margin: 5px auto 15px;
828
+ display: flex;
829
+ justify-content: space-between;
830
+ direction: rtl;
831
+ font-size: 14px;
832
+ font-family: 'B Nazanin', sans-serif;
833
+ font-weight: bold;
834
+ color: #6a0dad;
835
+ }}
836
+ .separator-line {{
837
+ width: 80%;
838
+ height: 2px;
839
+ background: #6a0dad;
840
+ margin: 20px auto;
841
+ }}
842
+ @media (max-width: 768px) {{
843
+ .likert-line {{
844
+ width: 90%;
845
+ }}
846
+ .likert-labels {{
847
+ width: 90%;
848
+ }}
849
+ }}
850
+ @media (min-width: 768px) {{
851
+ .likert-container {{
852
+ width: 475px;
853
+ margin: 0 auto;
854
+ }}
855
+ }}
856
+ </style>
857
 
858
+ <div class="likert-container">
859
+ <div class="likert-labels">
860
+ <span>{labels[0]}</span>
861
+ <span>{labels[1]}</span>
862
+ </div>
863
+ <div class="likert-line">
864
+ """
865
+
866
+ # اضافه کردن نقاط فقط برای مقادیر 1 تا scale
867
+ for i in range(1, scale + 1):
868
+ value = i
869
+ active_class = "active" if current_value == value else ""
870
+ scale_html += f"""
871
+ <div class='likert-dot {active_class}'
872
+ onclick='parent.window.parent.postMessage({{type: "update_selection", key: "{key}", value: {value}}}, "*")'>
873
+ </div>
874
+ """
875
+
876
+ scale_html += """
877
+ </div>
878
+ </div>
879
+ <script>
880
+ window.addEventListener('message', function(event) {
881
+ if (event.data.type === 'update_selection') {
882
+ fetch('http://localhost:5000/update_selection', {
883
+ method: 'POST',
884
+ headers: { 'Content-Type': 'application/json' },
885
+ body: JSON.stringify({ key: event.data.key, value: event.data.value })
886
+ }).then(response => response.json()).then(data => {
887
+ if (data.status === 'success') {
888
+ document.querySelectorAll('.likert-dot').forEach(dot => dot.classList.remove('active'));
889
+ let dots = document.querySelectorAll('.likert-dot');
890
+ dots[event.data.value - 1].classList.add('active');
891
+ }
892
+ });
893
+ }
894
+ });
895
+ </script>
896
+ """
897
+
898
+ # نمایش کامپوننت HTML
899
+ components.html(scale_html, height=60)
900
 
901
  # نمایش پاسخ انتخاب‌شده
902
+ if current_value != 0:
903
+ st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: {current_value}</p>",
904
  unsafe_allow_html=True)
905
  else:
906
  st.markdown(f"<p style='text-align:right; color:#6a0dad; direction: rtl;'>پاسخ شما: هنوز انتخاب نشده</p>",
907
  unsafe_allow_html=True)
908
 
909
+ # خط جداکننده بنفش
910
  st.markdown("""
911
  <style>
912
  .likert-separator {
 
916
  margin: 20px auto;
917
  direction: rtl;
918
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
919
  @media (max-width: 768px) {
920
  .likert-separator {
921
  width: 90%;
922
  }
 
 
 
 
 
 
 
 
 
 
 
923
  }
924
  </style>
925
  <div class="likert-separator"></div>
926
  """, unsafe_allow_html=True)
927
 
928
+ return current_value
929
 
930
  def create_ride_map():
931
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""