from starlette.routing import Mount def mount_if_not_exists(app, path: str, sub_app): """ Mounts a sub-application only if no Mount object already exists at the specified path in the main application's router. """ is_mounted = False for route in app.router.routes: if isinstance(route, Mount): if route.path == path: is_mounted = True break if not is_mounted: app.mount(path, app=sub_app)