Skip to content

production_server

production_server

Production server for trendify dashboards.

This module provides functions to run Dash applications in a production environment using the Waitress WSGI server.

run_production_server

run_production_server(
    app_or_collection: Union[Dash, DataProductCollection],
    host: str = "0.0.0.0",
    port: int = 8000,
    title: str = "Trendify Dashboard",
    debug: bool = False,
) -> None

Run a Dash application using a production WSGI server (Waitress).

Parameters:

Name Type Description Default
app_or_collection Union[Dash, DataProductCollection]

Either a Dash application or a DataProductCollection

required
host str

Host address to listen on

'0.0.0.0'
port int

Port to listen on

8000
title str

Title for the dashboard (used only if app_or_collection is a DataProductCollection)

'Trendify Dashboard'
debug bool

Enable debug mode (has no effect on Waitress but affects Dash)

False

Raises:

Type Description
ImportError

If Waitress is not installed

Source code in src/trendify/production_server.py
def run_production_server(
        app_or_collection: Union['dash.Dash', DataProductCollection],
        host: str = "0.0.0.0",
        port: int = 8000,
        title: str = "Trendify Dashboard",
        debug: bool = False,
    ) -> None:
    """
    Run a Dash application using a production WSGI server (Waitress).

    Args:
        app_or_collection: Either a Dash application or a DataProductCollection
        host: Host address to listen on
        port: Port to listen on
        title: Title for the dashboard (used only if app_or_collection is a DataProductCollection)
        debug: Enable debug mode (has no effect on Waitress but affects Dash)

    Raises:
        ImportError: If Waitress is not installed
    """
    try:
        import waitress
    except ImportError:
        print("Error: The 'waitress' package is required for production deployment.")
        print("Please install it with: pip install waitress")
        sys.exit(1)

    # Check if input is a Dash app or a DataProductCollection
    try:
        import dash
        if isinstance(app_or_collection, DataProductCollection):
            from trendify.plotly_dashboard import generate_plotly_dashboard
            app = generate_plotly_dashboard(app_or_collection, title, debug)
        elif isinstance(app_or_collection, dash.Dash):
            app = app_or_collection
        else:
            raise TypeError("app_or_collection must be either a Dash application or a DataProductCollection")
    except ImportError:
        print("Error: Dash is not installed. Please install it with: pip install dash")
        sys.exit(1)

    print(f"Starting production server with Waitress on {host}:{port}")
    waitress.serve(app.server, host=host, port=port)

serve_from_data_dir

serve_from_data_dir(
    data_dir: Union[str, Path],
    host: str = "0.0.0.0",
    port: int = 8000,
    title: Optional[str] = None,
    tag: Optional[str] = None,
) -> None

Serve a dashboard from a directory containing data products.

Parameters:

Name Type Description Default
data_dir Union[str, Path]

Path to directory containing data products

required
host str

Host address to listen on

'0.0.0.0'
port int

Port to listen on

8000
title Optional[str]

Optional title for the dashboard. If None, will use the directory name

None
tag Optional[str]

Optional tag to filter data products by

None
Source code in src/trendify/production_server.py
def serve_from_data_dir(
        data_dir: Union[str, Path],
        host: str = "0.0.0.0",
        port: int = 8000,
        title: Optional[str] = None,
        tag: Optional[str] = None,
    ) -> None:
    """
    Serve a dashboard from a directory containing data products.

    Args:
        data_dir: Path to directory containing data products
        host: Host address to listen on
        port: Port to listen on
        title: Optional title for the dashboard. If None, will use the directory name
        tag: Optional tag to filter data products by
    """
    from trendify.API import DataProductCollection

    data_dir = Path(data_dir)
    if not data_dir.exists():
        print(f"Error: Directory {data_dir} does not exist")
        sys.exit(1)

    if title is None:
        title = f"Trendify: {data_dir.name}"

    print(f"Loading data products from {data_dir}")
    try:
        collection = DataProductCollection.collect_from_all_jsons(data_dir, recursive=True)
        print(f"Loaded {len(collection.elements or [])} data products")

        if tag:
            filtered = collection.get_products(tag=tag)
            print(f"Filtered to {len(filtered.elements or [])} data products with tag '{tag}'")
            collection = filtered
    except Exception as e:
        print(f"Error loading data products: {e}")
        sys.exit(1)

    run_production_server(collection, host, port, title)