Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -739,7 +739,7 @@ h3 {
|
|
| 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"]
|
|
@@ -749,7 +749,7 @@ 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 = """
|
| 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>
|
|
@@ -764,6 +764,8 @@ def custom_likert_slider(question_data):
|
|
| 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 |
|
| 769 |
<script>
|
|
@@ -771,17 +773,19 @@ def custom_likert_slider(question_data):
|
|
| 771 |
const slider = document.getElementById(key);
|
| 772 |
const value = parseInt(slider.value);
|
| 773 |
document.getElementById('output_' + key).innerText = value;
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
|
|
|
|
|
|
| 785 |
}}
|
| 786 |
|
| 787 |
window.addEventListener('load', () => {{
|
|
@@ -790,24 +794,28 @@ def custom_likert_slider(question_data):
|
|
| 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=
|
| 801 |
|
| 802 |
-
#
|
| 803 |
-
if
|
| 804 |
-
current_value = st.session_state[key]
|
|
|
|
| 805 |
else:
|
| 806 |
current_value = st.session_state.get(key, default_value)
|
| 807 |
|
| 808 |
# برای دیباگ: نمایش مقدار فعلی
|
| 809 |
st.write(f"مقدار فعلی برای {key}: {current_value}")
|
| 810 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 811 |
return current_value
|
| 812 |
|
| 813 |
def create_ride_map():
|
|
|
|
| 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"]
|
|
|
|
| 749 |
default_value = (points + 1) // 2
|
| 750 |
current_value = st.session_state.get(key, default_value)
|
| 751 |
|
| 752 |
+
# HTML و JavaScript برای اسلایدر با دکمه تأیید
|
| 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>
|
|
|
|
| 764 |
<div style="text-align: center; margin-top: 10px; direction: rtl;">
|
| 765 |
پاسخ انتخابشده: <strong><span id="output_{0}">{5}</span></strong>
|
| 766 |
</div>
|
| 767 |
+
<button id="confirm_{0}" style="background-color: #6a0dad; color: white; border: none; border-radius: 8px; padding: 8px 16px; margin-top: 10px; cursor: pointer;"
|
| 768 |
+
onclick="confirmValue('{0}')">تأیید</button>
|
| 769 |
</div>
|
| 770 |
|
| 771 |
<script>
|
|
|
|
| 773 |
const slider = document.getElementById(key);
|
| 774 |
const value = parseInt(slider.value);
|
| 775 |
document.getElementById('output_' + key).innerText = value;
|
| 776 |
+
localStorage.setItem(key, value); // ذخیره موقت در localStorage
|
| 777 |
+
}}
|
| 778 |
+
|
| 779 |
+
function confirmValue(key) {{
|
| 780 |
+
const value = parseInt(document.getElementById(key).value);
|
| 781 |
+
// ارسال مقدار به Streamlit با یک متغیر مخفی
|
| 782 |
+
const hiddenInput = document.createElement('input');
|
| 783 |
+
hiddenInput.type = 'hidden';
|
| 784 |
+
hiddenInput.name = 'slider_value_' + key;
|
| 785 |
+
hiddenInput.value = value;
|
| 786 |
+
document.getElementById('container_' + key).appendChild(hiddenInput);
|
| 787 |
+
// کلیک کردن روی یک دکمه مخفی در Streamlit برای دریافت مقدار
|
| 788 |
+
window.parent.document.querySelector('[data-testid="stButton"] > button').click();
|
| 789 |
}}
|
| 790 |
|
| 791 |
window.addEventListener('load', () => {{
|
|
|
|
| 794 |
if (savedValue) {{
|
| 795 |
document.getElementById(key).value = savedValue;
|
| 796 |
document.getElementById('output_' + key).innerText = savedValue;
|
|
|
|
| 797 |
}}
|
| 798 |
}});
|
| 799 |
</script>
|
| 800 |
""".format(key, question, labels[0], labels[1], points, current_value)
|
| 801 |
|
| 802 |
# رندر کامپوننت HTML
|
| 803 |
+
components.html(html, height=200) # افزایش ارتفاع برای نمایش دکمه
|
| 804 |
|
| 805 |
+
# دریافت مقدار از فرم یا session_state
|
| 806 |
+
if st.session_state.get(f"slider_value_{key}", None) is not None:
|
| 807 |
+
current_value = st.session_state[f"slider_value_{key}"]
|
| 808 |
+
st.session_state[key] = current_value # ذخیره در session_state با کلید اصلی
|
| 809 |
else:
|
| 810 |
current_value = st.session_state.get(key, default_value)
|
| 811 |
|
| 812 |
# برای دیباگ: نمایش مقدار فعلی
|
| 813 |
st.write(f"مقدار فعلی برای {key}: {current_value}")
|
| 814 |
|
| 815 |
+
# دکمه مخفی برای دریافت مقدار (فعال میشه با کلیک روی دکمه تأیید)
|
| 816 |
+
if st.button("دریافت مقدار", key=f"hidden_btn_{key}", type="primary", help="این دکمه توسط JavaScript فعال میشه"):
|
| 817 |
+
pass # این فقط برای دریافت مقدار از hidden input استفاده میشه
|
| 818 |
+
|
| 819 |
return current_value
|
| 820 |
|
| 821 |
def create_ride_map():
|