kuroiikimono commited on
Commit
cf2c550
·
verified ·
1 Parent(s): 7f54275

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -1
app.py CHANGED
@@ -2,13 +2,21 @@ import streamlit as st
2
  import zipfile, shutil, time
3
  import os
4
  import hashlib
5
- from streamlit_pdf_viewer import pdf_viewer
6
  from streamlit import runtime
7
  from streamlit.runtime.scriptrunner import get_script_run_ctx
8
  from streamlit_js_eval import streamlit_js_eval
9
  import secrets
 
 
 
 
 
 
10
  from pypdf import PdfReader
11
  import glob
 
 
12
 
13
  def get_remote_ip() -> str:
14
  """Get remote ip."""
@@ -32,6 +40,49 @@ def my_makedirs(path):
32
  if not os.path.isdir(path):
33
  os.makedirs(path)
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  def main():
37
 
@@ -468,4 +519,5 @@ def main():
468
  # st.write(st.session_state.result, unsafe_allow_html=True)
469
 
470
  if __name__ == "__main__":
 
471
  main()
 
2
  import zipfile, shutil, time
3
  import os
4
  import hashlib
5
+ #from streamlit_pdf_viewer import pdf_viewer
6
  from streamlit import runtime
7
  from streamlit.runtime.scriptrunner import get_script_run_ctx
8
  from streamlit_js_eval import streamlit_js_eval
9
  import secrets
10
+
11
+ import threading
12
+ from streamlit.runtime.scriptrunner import add_script_run_ctx
13
+ import streamlit.components.v1 as components
14
+ from streamlit.runtime import get_instance
15
+
16
  from pypdf import PdfReader
17
  import glob
18
+ import logging
19
+
20
 
21
  def get_remote_ip() -> str:
22
  """Get remote ip."""
 
40
  if not os.path.isdir(path):
41
  os.makedirs(path)
42
 
43
+ def heart_beat():
44
+ """
45
+ Heartbeat function to track whether the session is alive
46
+ """
47
+ thread = threading.Timer(interval=2, function=heart_beat)
48
+
49
+ # insert context to the current thread, needed for
50
+ # getting session specific attributes like st.session_state
51
+
52
+ add_script_run_ctx(thread)
53
+
54
+ # context is required to get session_id of the calling
55
+ # thread (which would be the script thread)
56
+ ctx = get_script_run_ctx()
57
+
58
+ # this is the main runtime, contains all the sessions
59
+ runtime = get_instance()
60
+
61
+ if runtime.is_active_session(session_id=ctx.session_id):
62
+ logging.info(f"{ctx.session_id} is alive.")
63
+ thread.start()
64
+ else:
65
+ print(st.session_state.uniq)
66
+ if os.path.isdir(f"removefolder/{st.session_state.uniq}"):
67
+ shutil.rmtree(f"removefolder/{st.session_state.uniq}")
68
+ logging.info(f"{ctx.session_id} is gone.")
69
+ return
70
+
71
+ # JavaScript to detect browser exit
72
+ EXIT_JS = """
73
+ <script>
74
+ window.addEventListener('beforeunload', function (event) {
75
+ fetch('/close_session', {method: 'POST'}).then(response => {
76
+ return response.text();
77
+ }).then(data => {
78
+ console.log(data);
79
+ });
80
+ });
81
+ </script>
82
+ """
83
+
84
+ # Embed the JavaScript in the Streamlit app
85
+ components.html(EXIT_JS)
86
 
87
  def main():
88
 
 
519
  # st.write(st.session_state.result, unsafe_allow_html=True)
520
 
521
  if __name__ == "__main__":
522
+ heart_beat()
523
  main()