Repository: apache/superset
File: superset/app.py (Python)

Code before:
)
from superset.initialization import SupersetAppInitializer

logger = logging.getLogger(__name__)


def create_app(
    superset_config_module: Optional[str] = None,
    superset_app_root: Optional[str] = None,
) -> Flask:
    app = SupersetApp(__name__)

    try:
        # Allow user to override our config completely
        config_module = superset_config_module or os.environ.get(
            "SUPERSET_CONFIG", "superset.config"
        )
        app.config.from_object(config_module)

        # Allow application to sit on a non-root path
        # *Please be advised that this feature is in BETA.*
        app_root = cast(
            str, superset_app_root or os.environ.get("SUPERSET_APP_ROOT", "/")
        )
        if app_root != "/":
            app.wsgi_app = AppRootMiddleware(app.wsgi_app, app_root)
            # If not set, manually configure options that depend on the
            # value of app_root so things work out of the box
            if not app.config["STATIC_ASSETS_PREFIX"]:
                app.config["STATIC_ASSETS_PREFIX"] = app_root
            # Prefix APP_ICON path with subdirectory root for subdirectory deployments
            if (
                app.config.get("APP_ICON", "").startswith("/static/")
                and app_root != "/"
            ):
                app.config["APP_ICON"] = f"{app_root}{app.config['APP_ICON']}"
                # Also update theme tokens for subdirectory deployments
                for theme_key in ("THEME_DEFAULT", "THEME_DARK"):
                    theme = app.config[theme_key]
                    token = theme.get("token", {})
                    # Update brandLogoUrl if it points to /static/
                    if token.get("brandLogoUrl", "").startswith("/static/"):
                        token["brandLogoUrl"] = f"{app_root}{token['brandLogoUrl']}"
                    # Update brandLogoHref if it's the default "/"
                    if token.get("brandLogoHref") == "/":
                        token["brandLogoHref"] = app_root
            if app.config["APPLICATION_ROOT"] == "/":
                app.config["APPLICATION_ROOT"] = app_root

        app_initializer = app.config.get("APP_INITIALIZER", SupersetAppInitializer)(app)
        app_initializer.init_app()

Review comment (suggestion):
Flask has `APLICATION_ROOT = "/"` by default, so leaving it as the last option so we don't need a `get()` defaulting to `"/"`

Code after:
)
from superset.initialization import SupersetAppInitializer

logger = logging.getLogger(__name__)


def create_app(
    superset_config_module: Optional[str] = None,
    superset_app_root: Optional[str] = None,
) -> Flask:
    app = SupersetApp(__name__)

    try:
        # Allow user to override our config completely
        config_module = superset_config_module or os.environ.get(
            "SUPERSET_CONFIG", "superset.config"
        )
        app.config.from_object(config_module)

        # Allow application to sit on a non-root path
        # *Please be advised that this feature is in BETA.*
        app_root = cast(
            str,
            superset_app_root
            or os.environ.get("SUPERSET_APP_ROOT")
            or app.config["APPLICATION_ROOT"],
        )
        if app_root != "/":
            app.wsgi_app = AppRootMiddleware(app.wsgi_app, app_root)
            # If not set, manually configure options that depend on the
            # value of app_root so things work out of the box
            if not app.config["STATIC_ASSETS_PREFIX"]:
                app.config["STATIC_ASSETS_PREFIX"] = app_root
            # Prefix APP_ICON path with subdirectory root for subdirectory deployments
            if (
                app.config.get("APP_ICON", "").startswith("/static/")
                and app_root != "/"
            ):
                app.config["APP_ICON"] = f"{app_root}{app.config['APP_ICON']}"
                # Also update theme tokens for subdirectory deployments
                for theme_key in ("THEME_DEFAULT", "THEME_DARK"):
                    theme = app.config[theme_key]
                    token = theme.get("token", {})
                    # Update brandLogoUrl if it points to /static/
                    if token.get("brandLogoUrl", "").startswith("/static/"):
                        token["brandLogoUrl"] = f"{app_root}{token['brandLogoUrl']}"
                    # Update brandLogoHref if it's the default "/"
                    if token.get("brandLogoHref") == "/":
                        token["brandLogoHref"] = app_root
            if app.config["APPLICATION_ROOT"] == "/":
                app.config["APPLICATION_ROOT"] = app_root