From ce9b2e714cb0bededdb26f36b2ce395e8946968e Mon Sep 17 00:00:00 2001 From: ayushmaan Date: Thu, 13 Mar 2025 17:52:48 +0530 Subject: [PATCH 1/2] Stop/Close Button added --- app.py | 46 +++++++++++++++++++++++++++++++++++++++++++--- requirements.txt | 3 ++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 4952a8c80..20bd94d3a 100644 --- a/app.py +++ b/app.py @@ -3,10 +3,15 @@ import json # For some reason the windows version only works if this is imported here import pyopenms +import os +import time +import pyautogui +import psutil +import platform if "settings" not in st.session_state: - with open("settings.json", "r") as f: - st.session_state.settings = json.load(f) + with open("settings.json", "r") as f: + st.session_state.settings = json.load(f) if __name__ == '__main__': pages = { @@ -33,4 +38,39 @@ } pg = st.navigation(pages) - pg.run() \ No newline at end of file + pg.run() + +def close_app(): + """ + Closes the Streamlit app by terminating the Python process and + attempting to close the browser tab with keystrokes. + """ + with st.spinner("Shutting down..."): + time.sleep(3) # give the user a small window to see the spinner + + # Attempt to close the current browser tab (keystroke-based) + try: + if platform.system() == "Darwin": + # macOS typically uses 'command + w' + pyautogui.hotkey('command', 'w') + else: + # Windows/Linux typically use 'ctrl + w' + pyautogui.hotkey('ctrl', 'w') + except Exception as error: + st.warning( + "We tried closing the browser window, but failed. " + "You may need to close it manually. For macOS, ensure that:" + " System Preferences → Security & Privacy → Accessibility → Terminal is checked." + ) + + # Terminate the Streamlit python process + pid = os.getpid() + p = psutil.Process(pid) + p.terminate() + + # Place the “Stop/Close” button in the sidebar +with st.sidebar: + st.write("") # just an empty line for spacing + if st.button("Stop/Close"): + st.write("Terminating the app... please wait.") + close_app() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 95615786e..3236052f3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,5 @@ plotly==5.22.0 captcha==0.5.0 pyopenms_viz==1.0.0 streamlit-js-eval -psutil==7.0.0 \ No newline at end of file +psutil==7.0.0 +pyautogui==0.9.54 \ No newline at end of file From 792ccdf132c06f79de731f7709851e257fbec59c Mon Sep 17 00:00:00 2001 From: ayushmaan Date: Tue, 18 Mar 2025 00:25:03 +0530 Subject: [PATCH 2/2] Minor Fix --- environment.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index 188d5c118..0a08def6f 100644 --- a/environment.yml +++ b/environment.yml @@ -15,4 +15,5 @@ dependencies: - captcha==0.5.0 - pyopenms_viz==1.0.0 - streamlit-js-eval - - psutil==7.0.0 \ No newline at end of file + - psutil==7.0.0 + - pyautogui==0.9.54 \ No newline at end of file