Repository: plotly/dash
File: dash/_callback.py (Python)

Code before:
            the cache. This parameter only applies to background callbacks
            (`background=True`).
        :param interval:
            Time to wait between the background callback update requests.
        :param on_error:
            Function to call when the callback raises an exception. Receives the
            exception object as first argument. The callback_context can be used
            to access the original callback inputs, states and output.
        :param optional:
            Mark all dependencies as not required on the initial layout checks.
        :param hidden:
            Hide the callback from the devtools callbacks tab.
        :param api_endpoint:
            If provided, the callback will be available at the given API endpoint.
            This allows you to call the callback directly through HTTP requests
            instead of through the Dash front-end. The endpoint should be a string
            that starts with a forward slash (e.g. `/my_callback`).
            The endpoint is relative to the Dash app's base URL.
            Note that the endpoint will not appear in the list of registered
            callbacks in the Dash devtools.
    """

    background_spec: Any = None

    # Get prevent_initial_callbacks and hide_all_callbacks from get_app().config
    current_app_config = get_app().config

    config_prevent_initial_callbacks = current_app_config.get(
        "prevent_initial_callbacks", False
    )
    config_hide_all_callbacks = current_app_config.get("hide_all_callbacks", False)
    callback_map = _kwargs.pop("callback_map", GLOBAL_CALLBACK_MAP)
    callback_list = _kwargs.pop("callback_list", GLOBAL_CALLBACK_LIST)

    if background:
        background_spec = {
            "interval": interval,
        }

        if manager:
            background_spec["manager"] = manager

        if progress:
            background_spec["progress"] = coerce_to_list(progress)
            validate_background_inputs(background_spec["progress"])

        if progress_default:
            background_spec["progressDefault"] = coerce_to_list(progress_default)

            if not len(background_spec["progress"]) == len(
                background_spec["progressDefault"]

Review comment (suggestion):
Nice usage of get_app, but this will not work if the app hasn't been instanced yet which we can't guarantee with `dash.callback`.

We can defer it all and map the configs values in the _setup_server after this line: https://github.com/plotly/dash/blob/a9a173f7e27ab70e9a89b2c2afc13f8d107e3fe1/dash/dash.py#L1648

Code after:
            (`background=True`).
        :param interval:
            Time to wait between the background callback update requests.
        :param on_error:
            Function to call when the callback raises an exception. Receives the
            exception object as first argument. The callback_context can be used
            to access the original callback inputs, states and output.
        :param optional:
            Mark all dependencies as not required on the initial layout checks.
        :param hidden:
            Hide the callback from the devtools callbacks tab.
        :param api_endpoint:
            If provided, the callback will be available at the given API endpoint.
            This allows you to call the callback directly through HTTP requests
            instead of through the Dash front-end. The endpoint should be a string
            that starts with a forward slash (e.g. `/my_callback`).
            The endpoint is relative to the Dash app's base URL.
            Note that the endpoint will not appear in the list of registered
            callbacks in the Dash devtools.
    """

    background_spec: Any = None

    config_prevent_initial_callbacks = _kwargs.pop(
        "config_prevent_initial_callbacks", False
    )
    callback_map = _kwargs.pop("callback_map", GLOBAL_CALLBACK_MAP)
    callback_list = _kwargs.pop("callback_list", GLOBAL_CALLBACK_LIST)

    if background:
        background_spec = {
            "interval": interval,
        }

        if manager:
            background_spec["manager"] = manager

        if progress:
            background_spec["progress"] = coerce_to_list(progress)
            validate_background_inputs(background_spec["progress"])

        if progress_default:
            background_spec["progressDefault"] = coerce_to_list(progress_default)

            if not len(background_spec["progress"]) == len(
                background_spec["progressDefault"]
            ):
                raise Exception(
                    "Progress and progress default needs to be of same length"
                )