GSMEthesis commited on
Commit
d7c39f2
·
verified ·
1 Parent(s): bdf8aa9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -24
app.py CHANGED
@@ -749,20 +749,20 @@ def custom_likert_slider(question_data):
749
  default_value = (points + 1) // 2
750
  current_value = st.session_state.get(key, default_value)
751
 
752
- # HTML و JavaScript برای اسلایدر
753
- html = f"""
754
- <div id="container_{key}" style="direction: ltr; font-family: 'B Nazanin'; margin-bottom: 30px;">
755
- <label style="font-size: 16px; font-weight: bold; display: block; text-align: right;">{question}</label>
756
  <div style="display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 5px;">
757
- <span>{labels[0]}</span>
758
- <span>{labels[1]}</span>
759
  </div>
760
- <input type="range" id="{key}" min="1" max="{points}" step="1" value="{current_value}"
761
  style="width: 100%; height: 10px; accent-color: #6a0dad; margin-bottom: 15px;"
762
- oninput="updateSlider('{key}')"
763
- onchange="updateSlider('{key}')">
764
  <div style="text-align: center; margin-top: 10px; direction: rtl;">
765
- پاسخ انتخاب‌شده: <strong><span id="output_{key}">{current_value}</span></strong>
766
  </div>
767
  </div>
768
 
@@ -776,11 +776,7 @@ def custom_likert_slider(question_data):
776
  window.sliderValues[key] = value;
777
 
778
  try {{
779
- window.parent.postMessage({{
780
- type: 'streamlit:setComponentValue',
781
- key: "{key}",
782
- value: value
783
- }}, '*');
784
  }} catch (e) {{
785
  console.error('Error sending message to Streamlit:', e);
786
  }}
@@ -789,25 +785,21 @@ def custom_likert_slider(question_data):
789
  }}
790
 
791
  window.addEventListener('load', () => {{
792
- const key = '{key}';
793
  const savedValue = localStorage.getItem(key);
794
  if (savedValue) {{
795
  document.getElementById(key).value = savedValue;
796
  document.getElementById('output_' + key).innerText = savedValue;
797
- window.parent.postMessage({{
798
- type: 'streamlit:setComponentValue',
799
- key: "{key}",
800
- value: parseInt(savedValue)
801
- }}, '*');
802
  }}
803
- });
804
  </script>
805
- """
806
 
807
  # رندر کامپوننت HTML
808
  components.html(html, height=150)
809
 
810
- # خواندن مقدار از localStorage یا session_state
811
  if key in st.session_state:
812
  current_value = st.session_state[key]
813
  else:
 
749
  default_value = (points + 1) // 2
750
  current_value = st.session_state.get(key, default_value)
751
 
752
+ # HTML و JavaScript برای اسلایدر با استفاده از str.format
753
+ html = """
754
+ <div id="container_{0}" style="direction: ltr; font-family: 'B Nazanin'; margin-bottom: 30px;">
755
+ <label style="font-size: 16px; font-weight: bold; display: block; text-align: right;">{1}</label>
756
  <div style="display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 5px;">
757
+ <span>{2}</span>
758
+ <span>{3}</span>
759
  </div>
760
+ <input type="range" id="{0}" min="1" max="{4}" step="1" value="{5}"
761
  style="width: 100%; height: 10px; accent-color: #6a0dad; margin-bottom: 15px;"
762
+ oninput="updateSlider('{0}')"
763
+ onchange="updateSlider('{0}')">
764
  <div style="text-align: center; margin-top: 10px; direction: rtl;">
765
+ پاسخ انتخاب‌شده: <strong><span id="output_{0}">{5}</span></strong>
766
  </div>
767
  </div>
768
 
 
776
  window.sliderValues[key] = value;
777
 
778
  try {{
779
+ window.parent.postMessage({{type: 'streamlit:setComponentValue', key: "{0}", value: value}}, '*');
 
 
 
 
780
  }} catch (e) {{
781
  console.error('Error sending message to Streamlit:', e);
782
  }}
 
785
  }}
786
 
787
  window.addEventListener('load', () => {{
788
+ const key = '{0}';
789
  const savedValue = localStorage.getItem(key);
790
  if (savedValue) {{
791
  document.getElementById(key).value = savedValue;
792
  document.getElementById('output_' + key).innerText = savedValue;
793
+ window.parent.postMessage({{type: 'streamlit:setComponentValue', key: "{0}", value: parseInt(savedValue)}}, '*');
 
 
 
 
794
  }}
795
+ }});
796
  </script>
797
+ """.format(key, question, labels[0], labels[1], points, current_value)
798
 
799
  # رندر کامپوننت HTML
800
  components.html(html, height=150)
801
 
802
+ # خواندن مقدار از session_state
803
  if key in st.session_state:
804
  current_value = st.session_state[key]
805
  else: