Skip to content

Download Callbacks

download_callbacks

Download Callbacks - Data Export Functionality.

Provides download callbacks for all use cases following the pattern: - Extract raw data from merged-result-store - Apply use-case-specific data selection - Export via ResultExporter - Return dcc.send_bytes for browser download - Show toast notifications for feedback

Author: BioRemPP Development Team Date: 2025-11-28 Version: 1.0.0

Classes

DownloadCallbackFactory

DownloadCallbackFactory(config_path: Optional[Path] = None, rate_limit_seconds: float = 2.0)

Factory for creating download callbacks for use cases.

Uses factory pattern to generate standardized download callbacks based on use case configuration.

Initialize download callback factory.

Parameters:

Name Type Description Default
config_path Optional[Path]

Path to download_config.yaml file

None
rate_limit_seconds float

Minimum seconds between downloads per use case (default: 2.0)

2.0
Source code in src/presentation/callbacks/download_callbacks.py
def __init__(
    self, config_path: Optional[Path] = None, rate_limit_seconds: float = 2.0
):
    """
    Initialize download callback factory.

    Parameters
    ----------
    config_path : Optional[Path]
        Path to download_config.yaml file
    rate_limit_seconds : float
        Minimum seconds between downloads per use case (default: 2.0)
    """
    if config_path is None:
        # Default to infrastructure/config/download_config.yaml
        current_dir = Path(__file__).parent
        biorempp_root = current_dir.parent.parent
        config_path = (
            biorempp_root / "infrastructure" / "config" / "download_config.yaml"
        )

    self.config_path = config_path
    self.use_case_configs = self._load_config()
    self.exporter = ResultExporter()
    self.rate_limit_seconds = rate_limit_seconds
    self._last_download_time: Dict[str, float] = (
        {}
    )  # Track last download per use case

    logger.info(
        f"[DOWNLOAD] Initialized with {len(self.use_case_configs)} use case configs, rate limit: {rate_limit_seconds}s"
    )
Functions
register_all_callbacks
register_all_callbacks(app)

Register download callbacks for all configured use cases.

Parameters:

Name Type Description Default
app Dash

Dash application instance

required
Source code in src/presentation/callbacks/download_callbacks.py
def register_all_callbacks(self, app):
    """
    Register download callbacks for all configured use cases.

    Parameters
    ----------
    app : Dash
        Dash application instance
    """
    registered_count = 0

    for use_case_id, config in self.use_case_configs.items():
        try:
            self._register_use_case_callback(app, use_case_id, config)
            registered_count += 1
            logger.debug(f"[DOWNLOAD] Registered callback for {use_case_id}")
        except Exception as e:
            logger.error(f"[DOWNLOAD] Failed to register {use_case_id}: {e}")

    logger.info(f"[DOWNLOAD] Registered {registered_count} download callbacks")

Functions

register_download_callbacks

register_download_callbacks(app, config_path: Optional[Path] = None)

Register all download callbacks for the application.

This is the main entry point for registering download callbacks.

Parameters:

Name Type Description Default
app Dash

Dash application instance

required
config_path Optional[Path]

Path to download_config.yaml, by default None (uses default path)

None

Examples:

>>> from src.presentation.callbacks.download_callbacks import register_download_callbacks
>>> register_download_callbacks(app)
Notes
  • Call this function in biorempp_app.py after registering other callbacks
  • Requires download_config.yaml to be present
  • Automatically registers callbacks for all configured use cases
Source code in src/presentation/callbacks/download_callbacks.py
def register_download_callbacks(app, config_path: Optional[Path] = None):
    """
    Register all download callbacks for the application.

    This is the main entry point for registering download callbacks.

    Parameters
    ----------
    app : Dash
        Dash application instance
    config_path : Optional[Path], optional
        Path to download_config.yaml, by default None (uses default path)

    Examples
    --------
    >>> from src.presentation.callbacks.download_callbacks import register_download_callbacks
    >>> register_download_callbacks(app)

    Notes
    -----
    - Call this function in biorempp_app.py after registering other callbacks
    - Requires download_config.yaml to be present
    - Automatically registers callbacks for all configured use cases
    """
    factory = DownloadCallbackFactory(config_path)
    factory.register_all_callbacks(app)
    logger.info("[DOWNLOAD] All download callbacks registered successfully")