GSMEthesis commited on
Commit
51c9cf3
·
verified ·
1 Parent(s): 61f4fb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -48
app.py CHANGED
@@ -17,21 +17,6 @@ st.set_page_config(layout="wide", page_title="راهیار - تحلیل انصا
17
  SHEET_ID = "1mmdWAyOCYq4yXMgP53Duq712AnlqZWLkfIo76JqM7wM"
18
  SHEET_NAME = "Condition1"
19
 
20
-
21
- if 'component_values' not in st.session_state:
22
- st.session_state.component_values = {}
23
-
24
- def handle_component_message():
25
- messages = st.session_state.get('_component_messages', [])
26
- for message in messages:
27
- if message['type'] == 'streamlit:setComponentValue':
28
- if 'answers' not in st.session_state:
29
- st.session_state.answers = {}
30
- st.session_state.component_values[message['key']] = message['value']
31
- st.session_state[message['key']] = message['value']
32
- st.session_state.answers[message['key']] = message['value']
33
- st.rerun()
34
-
35
  # ========== استایل‌های سفارشی یکپارچه ==========
36
  st.markdown("""
37
  <style>
@@ -754,16 +739,19 @@ h3 {
754
 
755
  # ========== توابع اصلی ==========
756
  def custom_likert_slider(question_data):
757
- """نمایش سوال لیکرت با اسلایدر نقطه‌ای و لیبل‌های سفارشی"""
758
  question = question_data["question"]
759
  key = question_data["key"]
760
  points = question_data["scale"]
761
- labels = question_data.get("labels", ["کمترین", "بیشترین"])
762
 
763
  # مقدار پیش‌فرض (وسط طیف)
764
- default_value = st.session_state.get(key, (points + 1) // 2)
 
 
 
765
 
766
- # HTML و JavaScript
767
  html = f"""
768
  <div id="container_{key}" style="direction: ltr; font-family: 'B Nazanin'; margin-bottom: 30px;">
769
  <label style="font-size: 16px; font-weight: bold; display: block; text-align: right;">{question}</label>
@@ -771,41 +759,47 @@ def custom_likert_slider(question_data):
771
  <span>{labels[0]}</span>
772
  <span>{labels[1]}</span>
773
  </div>
774
- <input type="range" id="{key}" min="1" max="{points}" step="1" value="{default_value}"
775
  style="width: 100%; height: 10px; accent-color: #6a0dad; margin-bottom: 15px;"
776
  oninput="updateSlider('{key}')">
777
  <div style="text-align: center; margin-top: 10px; direction: rtl;">
778
- پاسخ انتخاب‌شده: <strong><span id="output_{key}">{default_value}</span></strong>
779
  </div>
780
  </div>
781
 
782
  <script>
783
  function updateSlider(key) {{
784
- const value = parseInt(document.getElementById(key).value);
 
785
  document.getElementById('output_' + key).innerText = value;
786
 
787
  // ارسال مقدار به Streamlit
788
  window.parent.postMessage({{
789
  type: 'streamlit:setComponentValue',
790
- key: key,
791
- value: value
792
  }}, '*');
 
 
 
793
  }}
794
-
795
- // اضافه کردن event listener برای ذخیره مقدار هنگام تغییر
796
- document.getElementById("{key}").addEventListener("change", function() {{
797
- updateSlider("{key}");
798
- }});
799
  </script>
800
  """
801
 
802
  # نمایش کامپوننت
803
  components.html(html, height=150)
804
 
805
- # بررسی اگر مقدار در session_state وجود دارد
806
- if key in st.session_state:
807
- return st.session_state[key]
808
- return default_value
 
 
 
809
 
810
  def create_ride_map():
811
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
@@ -1304,11 +1298,10 @@ def random_likert_questions():
1304
  </div>
1305
  """, unsafe_allow_html=True)
1306
 
 
1307
  for question in current_group['questions']:
1308
  answer = custom_likert_slider(question)
1309
- # ذخیره پاسخ در session_state
1310
- st.session_state[question["key"]] = answer
1311
- st.session_state.answers[question["key"]] = answer
1312
 
1313
 
1314
  # دکمه ادامه/اتمام
@@ -1520,21 +1513,13 @@ def main():
1520
  user_agent = st.query_params.get("user_agent", [""])[0]
1521
  st.session_state.is_desktop = "mobile" not in user_agent.lower()
1522
 
1523
-
1524
  if st.session_state.is_desktop:
1525
  # اطمینان از نمایش همان حالت موبایل برای همه دستگاه‌ها
1526
  st.session_state.is_desktop = False
1527
 
1528
- # مدیریت وضعیت اسلایدرها
1529
- if 'component_values' not in st.session_state:
1530
- st.session_state.component_values = {}
1531
-
1532
- # دریافت مقادیر از کامپوننت‌های HTML
1533
- component_values = st.session_state.get('component_values', {})
1534
- for key, value in component_values.items():
1535
- st.session_state[key] = value
1536
- if 'answers' in st.session_state:
1537
- st.session_state.answers[key] = value
1538
 
1539
  if 'current_page' not in st.session_state:
1540
  st.session_state.current_page = "welcome"
@@ -1544,7 +1529,6 @@ def main():
1544
  st.session_state.demographic_data = None
1545
  st.session_state.price_accepted = 0
1546
  st.session_state.attention_check1 = None
1547
- st.session_state.answers = {}
1548
 
1549
  pages = {
1550
  "welcome": welcome_page,
 
17
  SHEET_ID = "1mmdWAyOCYq4yXMgP53Duq712AnlqZWLkfIo76JqM7wM"
18
  SHEET_NAME = "Condition1"
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  # ========== استایل‌های سفارشی یکپارچه ==========
21
  st.markdown("""
22
  <style>
 
739
 
740
  # ========== توابع اصلی ==========
741
  def custom_likert_slider(question_data):
742
+ """نمایش سوال لیکرت با اسلایدر سفارشی و اطمینان از ذخیره مقدار"""
743
  question = question_data["question"]
744
  key = question_data["key"]
745
  points = question_data["scale"]
746
+ labels = question_data.get("labels", ["کمترین", "بیشترین"]) # لیبل‌های پیش‌فرض
747
 
748
  # مقدار پیش‌فرض (وسط طیف)
749
+ default_value = (points + 1) // 2
750
+
751
+ # مقدار فعلی از session_state
752
+ current_value = st.session_state.get(key, default_value)
753
 
754
+ # HTML و JavaScript برای اسلایدر
755
  html = f"""
756
  <div id="container_{key}" style="direction: ltr; font-family: 'B Nazanin'; margin-bottom: 30px;">
757
  <label style="font-size: 16px; font-weight: bold; display: block; text-align: right;">{question}</label>
 
759
  <span>{labels[0]}</span>
760
  <span>{labels[1]}</span>
761
  </div>
762
+ <input type="range" id="{key}" min="1" max="{points}" step="1" value="{current_value}"
763
  style="width: 100%; height: 10px; accent-color: #6a0dad; margin-bottom: 15px;"
764
  oninput="updateSlider('{key}')">
765
  <div style="text-align: center; margin-top: 10px; direction: rtl;">
766
+ پاسخ انتخاب‌شده: <strong><span id="output_{key}">{current_value}</span></strong>
767
  </div>
768
  </div>
769
 
770
  <script>
771
  function updateSlider(key) {{
772
+ const slider = document.getElementById(key);
773
+ const value = parseInt(slider.value);
774
  document.getElementById('output_' + key).innerText = value;
775
 
776
  // ارسال مقدار به Streamlit
777
  window.parent.postMessage({{
778
  type: 'streamlit:setComponentValue',
779
+ value: value,
780
+ key: 'custom_' + key
781
  }}, '*');
782
+
783
+ // به‌روزرسانی مقدار در input برای اطمینان
784
+ slider.value = value;
785
  }}
786
+
787
+ // تنظیم مقدار اولیه
788
+ document.getElementById('{key}').value = {current_value};
789
+ document.getElementById('output_{key}').innerText = {current_value};
 
790
  </script>
791
  """
792
 
793
  # نمایش کامپوننت
794
  components.html(html, height=150)
795
 
796
+ # دریافت مقدار از Streamlit
797
+ component_value = st.session_state.get(f"custom_{key}", current_value)
798
+
799
+ # ذخیره مقدار در session_state
800
+ st.session_state[key] = component_value
801
+
802
+ return component_value
803
 
804
  def create_ride_map():
805
  """ایجاد نقشه سفر با Folium - نسخه اصلاح شده با مناطق عمومی"""
 
1298
  </div>
1299
  """, unsafe_allow_html=True)
1300
 
1301
+ # نمایش تمام سوالات این گروه در یک صفحه
1302
  for question in current_group['questions']:
1303
  answer = custom_likert_slider(question)
1304
+ st.session_state.answers[question["key"]] = answer
 
 
1305
 
1306
 
1307
  # دکمه ادامه/اتمام
 
1513
  user_agent = st.query_params.get("user_agent", [""])[0]
1514
  st.session_state.is_desktop = "mobile" not in user_agent.lower()
1515
 
1516
+
1517
  if st.session_state.is_desktop:
1518
  # اطمینان از نمایش همان حالت موبایل برای همه دستگاه‌ها
1519
  st.session_state.is_desktop = False
1520
 
1521
+ if 'answers' not in st.session_state:
1522
+ st.session_state.answers = {}
 
 
 
 
 
 
 
 
1523
 
1524
  if 'current_page' not in st.session_state:
1525
  st.session_state.current_page = "welcome"
 
1529
  st.session_state.demographic_data = None
1530
  st.session_state.price_accepted = 0
1531
  st.session_state.attention_check1 = None
 
1532
 
1533
  pages = {
1534
  "welcome": welcome_page,