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

This commit is contained in:
pigeon 2023-07-18 03:45:48 +09:00 committed by GitHub
parent 7397cd795a
commit d6b191538e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

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

View File

@ -7,6 +7,7 @@ import os
import signal import signal
import subprocess import subprocess
import sys import sys
from datetime import datetime
from typing import Optional from typing import Optional
from urllib.parse import urlparse from urllib.parse import urlparse
@ -145,3 +146,14 @@ def new_process(args, **kwargs):
args, args,
**kwargs, **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}")