ankanghosh commited on
Commit
eedb5ab
·
verified ·
1 Parent(s): 18f6126

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -35
app.py CHANGED
@@ -36,49 +36,44 @@ if 'page_loaded' not in st.session_state:
36
  st.session_state.last_query = ""
37
  st.session_state.last_answer = None
38
 
39
- # Add new session state variable for detecting page reloads
40
- if 'page_visit_id' not in st.session_state:
41
- import uuid
42
- # Generate a unique ID for this page load
43
- st.session_state.page_visit_id = str(uuid.uuid4())
44
 
45
- # Use JavaScript to detect when user navigates away and back
46
- def setup_navigation_detection():
 
 
 
 
 
 
 
 
 
 
47
  components.html(
48
- f"""
49
  <script>
50
- (function() {{
51
- // Store the current page visit ID
52
- const currentVisitId = "{st.session_state.page_visit_id}";
53
-
54
- // Check if we have a stored ID from before
55
- const previousVisitId = localStorage.getItem('lastVisitId');
56
-
57
- // If we have a previous ID and it's different, we've returned after navigation
58
- if (previousVisitId && previousVisitId !== currentVisitId) {{
59
- console.log('Returned to app after navigation');
60
-
61
- // Force a reload of the page to reset all state
62
- window.location.reload();
63
- }}
64
-
65
- // Store current ID for next time
66
- localStorage.setItem('lastVisitId', currentVisitId);
67
-
68
- // Listen for navigation away from the page
69
- window.addEventListener('beforeunload', function() {{
70
- // When navigating away, we want to be able to detect return
71
- localStorage.setItem('navigatedAway', 'true');
72
- }});
73
- }})();
74
  </script>
75
  """,
76
  height=0
77
  )
78
 
79
- # Call this function early in your script
80
- setup_navigation_detection()
81
-
82
  # THEN: Import your modules
83
  from rag_engine import process_query, load_model, cached_load_data_files
84
  from utils import setup_all_auth
 
36
  st.session_state.last_query = ""
37
  st.session_state.last_answer = None
38
 
39
+ # Add a flag to track if we need to check for navigation
40
+ if 'check_navigation' not in st.session_state:
41
+ st.session_state.check_navigation = True
 
 
42
 
43
+ # Check if we need to reset state on page load
44
+ if st.session_state.check_navigation:
45
+ # Reset key query-related state variables
46
+ st.session_state.last_query = ""
47
+ st.session_state.last_answer = None
48
+ st.session_state.submit_clicked = False
49
+ # If you have any other state to reset, do it here
50
+
51
+ # Mark that we've checked navigation for this page load
52
+ st.session_state.check_navigation = False
53
+
54
+ # Add JavaScript to reset the flag when navigating away
55
  components.html(
56
+ """
57
  <script>
58
+ // Set up beforeunload event to prepare for next page load
59
+ window.addEventListener('beforeunload', function() {
60
+ // This will be executed when the user navigates away
61
+ // We'll use sessionStorage which persists across page loads
62
+ sessionStorage.setItem('resetOnReturn', 'true');
63
+ });
64
+
65
+ // Check if we need to reload on return
66
+ if (sessionStorage.getItem('resetOnReturn') === 'true') {
67
+ // Clear the flag
68
+ sessionStorage.removeItem('resetOnReturn');
69
+ // Force page reload to reset state
70
+ window.location.reload();
71
+ }
 
 
 
 
 
 
 
 
 
 
72
  </script>
73
  """,
74
  height=0
75
  )
76
 
 
 
 
77
  # THEN: Import your modules
78
  from rag_engine import process_query, load_model, cached_load_data_files
79
  from utils import setup_all_auth