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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py CHANGED
@@ -36,6 +36,49 @@ if 'page_loaded' not in st.session_state:
36
  st.session_state.last_query = ""
37
  st.session_state.last_answer = None
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  # THEN: Import your modules
40
  from rag_engine import process_query, load_model, cached_load_data_files
41
  from utils import setup_all_auth
 
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