Skip to content

Conversation

@Ayushmaan06
Copy link

@Ayushmaan06 Ayushmaan06 commented Mar 13, 2025

Hey @Arslan-Siraj
This pr solves issue (#151) using a stop/close button.
This is not automated, but it follows the suggestion of using approach made by MassDash.

Summary by CodeRabbit

  • New Features

    • Introduced a sidebar "Stop/Close" button that lets you gracefully shut down the application. During shutdown, you’ll see a visual indicator and, if needed, receive guidance for resolving any issues.
  • Chores

    • Added new dependencies to support the shutdown functionality, including pyautogui.

@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2025

Walkthrough

The pull request introduces a new function close_app() in app.py which programmatically terminates the Streamlit application. The function uses a shutdown spinner, OS-specific keyboard shortcuts to close the browser tab, and displays a warning message if the closure attempt fails. It then retrieves the process ID and terminates the application using the psutil library. A sidebar button labeled "Stop/Close" now triggers this function. Additionally, the pyautogui==0.9.54 package has been added to both requirements.txt and environment.yml, and an indentation issue in settings loading was corrected.

Changes

File Change Summary
app.py Added function close_app() to close the app programmatically with OS-specific keyboard shortcuts, spinner, warning message on failure, process termination, and fixed settings loading indentation.
requirements.txt Added package pyautogui==0.9.54 alongside the existing psutil==7.0.0.
environment.yml Added dependency pyautogui==0.9.54 while retaining psutil==7.0.0.

Sequence Diagram(s)

sequenceDiagram
  participant U as User
  participant S as Sidebar Button
  participant A as close_app()
  participant OS as Operating System
  participant P as Process Manager

  U->>S: Click "Stop/Close"
  S->>A: Trigger close_app()
  A->>A: Display shutdown spinner
  A->>OS: Execute keyboard shortcut to close tab
  alt Tab closes successfully
    A->>P: Terminate Streamlit process using psutil
  else Tab fails to close
    A->>U: Display warning message (macOS instructions)
    A->>P: Terminate Streamlit process using psutil
  end
Loading

Poem

I'm a coding rabbit, swift and keen,
Hopping through functions, a shutdown machine.
I press the button with a little flair,
Closing apps with style and care.
With bugs out and processes done,
I celebrate code under the digital sun!
🐇✨

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 887157c and ce9b2e7.

📒 Files selected for processing (2)
  • app.py (2 hunks)
  • requirements.txt (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
app.py

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

Remove assignment to unused variable error

(F841)

🔇 Additional comments (4)
requirements.txt (1)

12-12: PyAutoGUI dependency added for application close functionality.

The addition of pyautogui==0.9.54 is appropriate for the new close application functionality, where it's used to simulate keyboard shortcuts to close browser tabs.

app.py (3)

6-10: LGTM: New imports added for app termination functionality.

The added imports are necessary for the new close application feature:

  • os for process ID retrieval
  • time for spinner delay
  • pyautogui for browser tab closing via keyboard shortcuts
  • psutil for process termination
  • platform for OS detection

13-14: LGTM: Indentation fixed for settings loading.

The indentation correction ensures proper code structure when loading settings from the JSON file.


72-76: LGTM: Sidebar close button implementation.

The "Stop/Close" button in the sidebar with appropriate messaging meets the requirements specified in the PR objective. The button triggers the close_app() function to terminate the application.

Comment on lines +43 to +70
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()

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)

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

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
environment.yml (1)

18-19: New Dependency Addition and File Formatting Issue

The additions of psutil==7.0.0 and pyautogui==0.9.54 align with the PR’s objective of enhancing functionality for the Stop/Close button. However, please add a newline character at the end of the file to comply with YAML lint standards.

🧰 Tools
🪛 YAMLlint (1.35.1)

[error] 19-19: no new line character at the end of file

(new-line-at-end-of-file)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ce9b2e7 and 37035a2.

📒 Files selected for processing (1)
  • environment.yml (1 hunks)
🧰 Additional context used
🪛 YAMLlint (1.35.1)
environment.yml

[error] 19-19: no new line character at the end of file

(new-line-at-end-of-file)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows executable developed with wix: Closing the web browser does not terminate the streamlit process

2 participants