Maryam Ilka commited on
Commit
24af8b2
·
verified ·
1 Parent(s): 4eb03cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -34
app.py CHANGED
@@ -2,6 +2,9 @@ import streamlit as st
2
  import folium
3
  from streamlit_folium import folium_static
4
  from streamlit.components.v1 import html
 
 
 
5
  from datetime import datetime
6
  import gspread
7
  from google.oauth2.service_account import Credentials
@@ -601,41 +604,23 @@ def attention_check1():
601
  key="att1_radio"
602
  )
603
 
604
- # 1. تزریق JavaScript برای ارتباط دوطرفه
605
- js_code = """
606
- <script>
607
- function handleClick() {
608
- // روش 1: ارسال به sessionState
609
- Streamlit.setComponentValue("btn_clicked");
610
-
611
- // روش 2: یا از طریق postMessage
612
- // window.parent.postMessage("continue_green_clicked", "*");
613
- }
614
- </script>
615
-
616
- <button onclick="handleClick()"
617
- style="background-color:#28a745; color:white; border:none;
618
- border-radius:8px; padding:10px 20px; font-weight:bold;
619
- width:100%; cursor:pointer; margin-top:10px;">
620
- ادامه
621
- </button>
622
- """
623
- html(js_code, height=60)
624
-
625
- # 2. بررسی کلیک
626
- if st.session_state.get("btn_clicked"):
627
- if answer:
628
- st.session_state.attention_check1 = answer
629
- st.session_state.current_page = "random_likert_questions"
630
- time.sleep(0.1) # تأخیر کوتاه برای اطمینان
631
- st.rerun()
632
- else:
633
- st.warning("لطفاً یک گزینه را انتخاب کنید")
634
- st.session_state.btn_clicked = False # ریست وضعیت
635
 
636
- # 3. مقداردهی اولیه session state
637
- if "btn_clicked" not in st.session_state:
638
- st.session_state.btn_clicked = False
 
 
 
 
 
 
 
 
 
 
 
639
 
640
 
641
  def attention_check2():
@@ -930,4 +915,14 @@ def main():
930
  pages[st.session_state.current_page]()
931
 
932
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
933
  main()
 
2
  import folium
3
  from streamlit_folium import folium_static
4
  from streamlit.components.v1 import html
5
+ import custom_button
6
+ from streamlit.runtime.scriptrunner import RerunData, RerunException
7
+ from streamlit.source_util import get_pages
8
  from datetime import datetime
9
  import gspread
10
  from google.oauth2.service_account import Credentials
 
604
  key="att1_radio"
605
  )
606
 
607
+ # استفاده از کامپوننت اختصاصی
608
+ custom_button("ادامه", key="continue_btn")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
609
 
610
+ # بررسی کلیک
611
+ if st.session_state.get("continue_btn_clicked"):
612
+ if not answer:
613
+ st.warning("لطفاً یک گزینه را انتخاب کنید")
614
+ else:
615
+ st.session_state.attention_check1 = answer
616
+ # تغییر صفحه به صورت دستی
617
+ def update_page():
618
+ pages = get_pages("")
619
+ pages["random_likert_questions"].is_active = True
620
+ pages["current_page"].is_active = False
621
+ raise RerunException(RerunData())
622
+
623
+ update_page()
624
 
625
 
626
  def attention_check2():
 
915
  pages[st.session_state.current_page]()
916
 
917
  if __name__ == "__main__":
918
+ # دریافت پیام از دکمه
919
+ if "continue_btn_clicked" not in st.session_state:
920
+ st.session_state.continue_btn_clicked = False
921
+
922
+ # دریافت پیام از iframe
923
+ from streamlit.web.server.websocket_headers import _get_websocket_headers
924
+ headers = _get_websocket_headers()
925
+ if headers and headers.get("Referer"):
926
+ if "continue_btn_clicked" in headers.get("Referer"):
927
+ st.session_state.continue_btn_clicked = True
928
  main()