Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -33,4 +38,39 @@
}

pg = st.navigation(pages)
pg.run()
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()

Comment on lines +43 to +70
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Resolve unused exception variable and consider environment compatibility.

The close_app() function implementation generally looks good, but there are a couple of issues to address:

  1. The error variable in the exception handler is assigned but never used.
  2. PyAutoGUI's keyboard shortcuts might not work in all environments, especially in headless servers or containerized deployments.

Fix the unused variable issue:

-        except Exception as error:
+        except Exception:

Additionally, consider adding a comment about the environment limitations:

    """
    Closes the Streamlit app by terminating the Python process and
    attempting to close the browser tab with keystrokes.
+   
+   Note: Tab closing via keyboard shortcuts requires a GUI environment
+   and may not work in headless servers or containerized deployments.
    """
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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()
def close_app():
"""
Closes the Streamlit app by terminating the Python process and
attempting to close the browser tab with keystrokes.
Note: Tab closing via keyboard shortcuts requires a GUI environment
and may not work in headless servers or containerized deployments.
"""
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:
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()
🧰 Tools
🪛 Ruff (0.8.2)

59-59: Local variable error is assigned to but never used

Remove assignment to unused variable error

(F841)

# 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()
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ dependencies:
- captcha==0.5.0
- pyopenms_viz==1.0.0
- streamlit-js-eval
- psutil==7.0.0
- psutil==7.0.0
- pyautogui==0.9.54
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ plotly==5.22.0
captcha==0.5.0
pyopenms_viz==1.0.0
streamlit-js-eval
psutil==7.0.0
psutil==7.0.0
pyautogui==0.9.54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done sir