streamlit_healthcheck.streamlit-healthcheck-cli
1import click 2import logging 3import sys 4from typing import Optional 5from .server import start_api_server 6""" 7Streamlit Health Check CLI 8This module provides a command-line interface (CLI) for monitoring the health of Streamlit applications. 9It offers commands to start an API server for health checks and to initialize a new health check configuration file. 10Commands: 11--------- 12- serve: 13 Starts the health check API server. 14 Options: 15 --host: Host address to bind the server (default: '0.0.0.0') 16 --port: Port to run the server on (default: 8000) 17 --config: Path to health check configuration file (default: 'config/health_check_config.json') 18 --log-level: Set the logging level (choices: DEBUG, INFO, WARNING, ERROR, CRITICAL; default: INFO) 19- init: 20 Initializes a new health check configuration file. 21 Options: 22 --config: Path to health check configuration file (default: 'config/health_check_config.json') 23Usage: 24------ 25Run the CLI using the following command: 26 python streamlit-healthcheck-cli.py [COMMAND] [OPTIONS] 27Example: 28-------- 29 python streamlit-healthcheck-cli.py serve --host 127.0.0.1 --port 8080 --config my_config.json --log-level DEBUG 30 python streamlit-healthcheck-cli.py init --config my_config.json 31Logging: 32-------- 33Logging is configured to output to stdout with customizable log levels. 34Error Handling: 35--------------- 36All commands handle exceptions gracefully and provide informative error messages. 37""" 38# Configure logging 39logging.basicConfig( 40 level=logging.INFO, 41 format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', 42 handlers=[ 43 logging.StreamHandler(sys.stdout) 44 ] 45) 46logger = logging.getLogger(__name__) 47 48 49 50@click.group() 51def cli(): 52 """Streamlit Health Check CLI - Monitor your Streamlit application health""" 53 pass 54 55@cli.command() 56@click.option( 57 '--host', 58 default='0.0.0.0', 59 help='Host address to bind the server' 60) 61@click.option( 62 '--port', 63 default=8000, 64 type=int, 65 help='Port to run the server on' 66) 67@click.option( 68 '--config', 69 default='config/health_check_config.json', 70 type=click.Path(), 71 help='Path to health check configuration file' 72) 73@click.option( 74 '--log-level', 75 default='INFO', 76 type=click.Choice(['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], case_sensitive=False), 77 help='Set the logging level' 78) 79def serve(host: str, port: int, config: str, log_level: str): 80 """Start the health check API server""" 81 try: 82 # Set logging level 83 logging.getLogger().setLevel(log_level.upper()) 84 85 click.echo(f"Starting health check API server on {host}:{port}") 86 click.echo(f"Using config file: {config}") 87 click.echo(f"Log level: {log_level}") 88 89 # Start the server 90 start_api_server(host=host, port=port, config=config) 91 92 except Exception as e: 93 logger.error(f"Failed to start server: {str(e)}") 94 raise click.ClickException(str(e)) 95 96@cli.command() 97@click.option( 98 '--config', 99 default='config/health_check_config.json', 100 type=click.Path(), 101 help='Path to health check configuration file' 102) 103def init(config: str): 104 """Initialize a new health check configuration file""" 105 from .healthcheck import HealthCheckService 106 107 try: 108 service = HealthCheckService(config_path=config) 109 service.save_config() 110 click.echo(f"Created new configuration file at: {config}") 111 112 except Exception as e: 113 logger.error(f"Failed to create config file: {str(e)}") 114 raise click.ClickException(str(e)) 115 116def main(): 117 """ 118 Entry point for the Streamlit Healthcheck CLI. 119 Attempts to execute the CLI command and handles any unexpected exceptions 120 by logging the error and exiting the program with a non-zero status code. 121 """ 122 123 try: 124 cli() 125 except Exception as e: 126 logger.error(f"Unexpected error: {str(e)}") 127 sys.exit(1) 128 129if __name__ == '__main__': 130 main()
logger =
<Logger streamlit_healthcheck.streamlit-healthcheck-cli (INFO)>
cli =
<Group cli>
Streamlit Health Check CLI - Monitor your Streamlit application health
serve =
<Command serve>
Start the health check API server
init =
<Command init>
Initialize a new health check configuration file
def
main():
117def main(): 118 """ 119 Entry point for the Streamlit Healthcheck CLI. 120 Attempts to execute the CLI command and handles any unexpected exceptions 121 by logging the error and exiting the program with a non-zero status code. 122 """ 123 124 try: 125 cli() 126 except Exception as e: 127 logger.error(f"Unexpected error: {str(e)}") 128 sys.exit(1)
Entry point for the Streamlit Healthcheck CLI. Attempts to execute the CLI command and handles any unexpected exceptions by logging the error and exiting the program with a non-zero status code.