Преглед изворни кода

Add custom message when the user exits an app (#1345)

pigeon пре 1 година
родитељ
комит
d6b191538e
2 измењених фајлова са 16 додато и 0 уклоњено
  1. 4 0
      reflex/reflex.py
  2. 12 0
      reflex/utils/processes.py

+ 4 - 0
reflex/reflex.py

@@ -2,6 +2,7 @@
 
 import os
 import platform
+import signal
 import threading
 from pathlib import Path
 
@@ -163,6 +164,9 @@ def run(
             args=(app.__name__, backend_host, backend_port, loglevel),
         ).start()
 
+    # Display custom message when there is a keyboard interrupt.
+    signal.signal(signal.SIGINT, processes.catch_keyboard_interrupt)
+
 
 @cli.command()
 def deploy(dry_run: bool = typer.Option(False, help="Whether to run a dry run.")):

+ 12 - 0
reflex/utils/processes.py

@@ -7,6 +7,7 @@ import os
 import signal
 import subprocess
 import sys
+from datetime import datetime
 from typing import Optional
 from urllib.parse import urlparse
 
@@ -145,3 +146,14 @@ def new_process(args, **kwargs):
         args,
         **kwargs,
     )
+
+
+def catch_keyboard_interrupt(signal, frame):
+    """Display a custom message with the current time when exiting an app.
+
+    Args:
+        signal: The keyboard interrupt signal.
+        frame: The current stack frame.
+    """
+    current_time = datetime.now().isoformat()
+    console.print(f"\nReflex app stopped at time: {current_time}")