rework server startup and cli
Some checks failed
Some checks failed
This changes the dockerfile as well.
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
import dash
|
||||
import dash_bootstrap_components as dbc
|
||||
from .config.settings import AppSettings
|
||||
from .ui.layout import AppLayout
|
||||
from .ui.callbacks.data_processing import DataProcessingCallbacks
|
||||
from .ui.callbacks.visualization import VisualizationCallbacks
|
||||
from .ui.callbacks.interactions import InteractionCallbacks
|
||||
"""
|
||||
EmbeddingBuddy application factory and server functions.
|
||||
|
||||
This module contains the main application creation logic with imports
|
||||
moved inside functions to avoid loading heavy dependencies at module level.
|
||||
"""
|
||||
|
||||
|
||||
def create_app():
|
||||
"""Create and configure the Dash application instance."""
|
||||
import os
|
||||
import dash
|
||||
import dash_bootstrap_components as dbc
|
||||
from .ui.layout import AppLayout
|
||||
from .ui.callbacks.data_processing import DataProcessingCallbacks
|
||||
from .ui.callbacks.visualization import VisualizationCallbacks
|
||||
from .ui.callbacks.interactions import InteractionCallbacks
|
||||
|
||||
# Get the project root directory (two levels up from this file)
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
|
||||
@@ -124,6 +130,9 @@ def _register_client_side_callbacks(app):
|
||||
|
||||
|
||||
def run_app(app=None, debug=None, host=None, port=None):
|
||||
"""Run the Dash application with specified settings."""
|
||||
from .config.settings import AppSettings
|
||||
|
||||
if app is None:
|
||||
app = create_app()
|
||||
|
||||
@@ -134,10 +143,69 @@ def run_app(app=None, debug=None, host=None, port=None):
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
"""Main entry point for the embeddingbuddy CLI command."""
|
||||
def serve(host=None, port=None, dev=False, debug=False):
|
||||
"""Start the EmbeddingBuddy web server.
|
||||
|
||||
Args:
|
||||
host: Host to bind to (default: 127.0.0.1)
|
||||
port: Port to bind to (default: 8050)
|
||||
dev: Development mode - enable debug logging and auto-reload (default: False)
|
||||
debug: Enable debug logging only, no auto-reload (default: False)
|
||||
"""
|
||||
import os
|
||||
from .config.settings import AppSettings
|
||||
|
||||
# Determine actual values to use
|
||||
actual_host = host if host is not None else AppSettings.HOST
|
||||
actual_port = port if port is not None else AppSettings.PORT
|
||||
|
||||
# Determine mode
|
||||
# --dev takes precedence and enables both debug and auto-reload
|
||||
# --debug enables only debug logging
|
||||
# No flags = production mode (no debug, no auto-reload)
|
||||
use_reloader = dev
|
||||
use_debug = dev or debug
|
||||
|
||||
# Only print startup messages in main process (not in Flask reloader)
|
||||
if not os.environ.get('WERKZEUG_RUN_MAIN'):
|
||||
mode = "development" if dev else ("debug" if debug else "production")
|
||||
print(f"Starting EmbeddingBuddy in {mode} mode...")
|
||||
print("Loading dependencies (this may take a few seconds)...")
|
||||
print(f"Server will start at http://{actual_host}:{actual_port}")
|
||||
if use_reloader:
|
||||
print("Auto-reload enabled - server will restart on code changes")
|
||||
|
||||
app = create_app()
|
||||
run_app(app)
|
||||
|
||||
# Suppress Flask development server warning in production mode
|
||||
if not use_debug and not use_reloader:
|
||||
import warnings
|
||||
import logging
|
||||
|
||||
# Suppress the werkzeug warning
|
||||
warnings.filterwarnings('ignore', message='.*development server.*')
|
||||
|
||||
# Set werkzeug logger to ERROR level to suppress the warning
|
||||
werkzeug_logger = logging.getLogger('werkzeug')
|
||||
werkzeug_logger.setLevel(logging.ERROR)
|
||||
|
||||
# Use Flask's built-in server with appropriate settings
|
||||
app.run(
|
||||
debug=use_debug,
|
||||
host=actual_host,
|
||||
port=actual_port,
|
||||
use_reloader=use_reloader
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
"""Legacy entry point - redirects to cli module.
|
||||
|
||||
This is kept for backward compatibility but the main CLI
|
||||
is now in embeddingbuddy.cli for faster startup.
|
||||
"""
|
||||
from .cli import main as cli_main
|
||||
cli_main()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
74
src/embeddingbuddy/cli.py
Normal file
74
src/embeddingbuddy/cli.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""
|
||||
Lightweight CLI entry point for EmbeddingBuddy.
|
||||
|
||||
This module provides a fast command-line interface that only imports
|
||||
heavy dependencies when actually needed by subcommands.
|
||||
"""
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
"""Main CLI entry point with minimal imports for fast help text."""
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="embeddingbuddy",
|
||||
description="EmbeddingBuddy - Interactive embedding visualization tool",
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
epilog="""
|
||||
Examples:
|
||||
embeddingbuddy serve # Production mode (no debug, no auto-reload)
|
||||
embeddingbuddy serve --dev # Development mode (debug + auto-reload)
|
||||
embeddingbuddy serve --debug # Debug logging only (no auto-reload)
|
||||
embeddingbuddy serve --port 8080 # Custom port
|
||||
embeddingbuddy serve --host 0.0.0.0 # Bind to all interfaces
|
||||
"""
|
||||
)
|
||||
|
||||
subparsers = parser.add_subparsers(
|
||||
dest="command",
|
||||
help="Available commands",
|
||||
metavar="<command>"
|
||||
)
|
||||
|
||||
# Serve subcommand
|
||||
serve_parser = subparsers.add_parser(
|
||||
"serve",
|
||||
help="Start the web server",
|
||||
description="Start the EmbeddingBuddy web server for interactive visualization"
|
||||
)
|
||||
serve_parser.add_argument(
|
||||
"--host",
|
||||
default=None,
|
||||
help="Host to bind to (default: 127.0.0.1)"
|
||||
)
|
||||
serve_parser.add_argument(
|
||||
"--port",
|
||||
type=int,
|
||||
default=None,
|
||||
help="Port to bind to (default: 8050)"
|
||||
)
|
||||
serve_parser.add_argument(
|
||||
"--dev",
|
||||
action="store_true",
|
||||
help="Development mode: enable debug logging and auto-reload"
|
||||
)
|
||||
serve_parser.add_argument(
|
||||
"--debug",
|
||||
action="store_true",
|
||||
help="Enable debug logging (no auto-reload)"
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.command == "serve":
|
||||
# Only import heavy dependencies when actually running serve
|
||||
from embeddingbuddy.app import serve
|
||||
serve(host=args.host, port=args.port, dev=args.dev, debug=args.debug)
|
||||
else:
|
||||
# No command specified, show help
|
||||
parser.print_help()
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
@@ -69,7 +69,7 @@ class AppSettings:
|
||||
TEXT_PREVIEW_LENGTH = 100
|
||||
|
||||
# App Configuration
|
||||
DEBUG = os.getenv("EMBEDDINGBUDDY_DEBUG", "True").lower() == "true"
|
||||
DEBUG = os.getenv("EMBEDDINGBUDDY_DEBUG", "False").lower() == "true"
|
||||
HOST = os.getenv("EMBEDDINGBUDDY_HOST", "127.0.0.1")
|
||||
PORT = int(os.getenv("EMBEDDINGBUDDY_PORT", "8050"))
|
||||
|
||||
|
11
src/embeddingbuddy/wsgi.py
Normal file
11
src/embeddingbuddy/wsgi.py
Normal file
@@ -0,0 +1,11 @@
|
||||
"""
|
||||
WSGI entry point for production deployment.
|
||||
Use this with a production WSGI server like Gunicorn.
|
||||
"""
|
||||
from embeddingbuddy.app import create_app
|
||||
|
||||
# Create the application instance
|
||||
application = create_app()
|
||||
|
||||
# For compatibility with different WSGI servers
|
||||
app = application
|
Reference in New Issue
Block a user