Skip to content

Commit 81f148f

Browse files
committed
init app check instead of backend blueprint check
1 parent f02a947 commit 81f148f

4 files changed

Lines changed: 10 additions & 13 deletions

File tree

dash/backends/_fastapi.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,6 @@ def create_app(name: str = "__main__", config: Dict[str, Any] | None = None):
272272
def register_assets_blueprint(
273273
self, blueprint_name: str, assets_url_path: str, assets_folder: str
274274
):
275-
# Check if route is already mounted to avoid duplicate registration
276-
# This can happen when init_app() is called multiple times
277-
for route in self.server.routes:
278-
if getattr(route, "name", None) == blueprint_name:
279-
return
280275
try:
281276
self.server.mount(
282277
assets_url_path,

dash/backends/_flask.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ def create_app(name: str = "__main__", config: Dict[str, Any] | None = None):
8686
def register_assets_blueprint(
8787
self, blueprint_name: str, assets_url_path: str, assets_folder: str
8888
):
89-
# Check if blueprint is already registered to avoid duplicate registration
90-
# This can happen when init_app() is called multiple times (e.g., with flask run)
91-
if blueprint_name in self.server.blueprints:
92-
return
9389
bp = Blueprint(
9490
blueprint_name,
9591
__name__,

dash/backends/_quart.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ def create_app(
122122
def register_assets_blueprint(
123123
self, blueprint_name: str, assets_url_path: str, assets_folder: str # type: ignore[name-defined]
124124
):
125-
# Check if blueprint is already registered to avoid duplicate registration
126-
# This can happen when init_app() is called multiple times (e.g., with quart run)
127-
if blueprint_name in self.server.blueprints:
128-
return
129125
bp = Blueprint(
130126
blueprint_name,
131127
__name__,

dash/dash.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@
135135
_ID_STORE = "_pages_store"
136136
_ID_DUMMY = "_pages_dummy"
137137

138+
_UNINITIALIZED = object() # Sentinel for tracking init_app state
139+
138140
DASH_VERSION_URL = "https://dash-version.plotly.com:8080/current_version"
139141

140142
# Handles the case in a newly cloned environment where the components are not yet generated.
@@ -734,6 +736,14 @@ def init_app(self, app: Optional[Any] = None, **kwargs) -> None:
734736
# Also update the backend's server reference so routes are registered
735737
# on the correct server (important when using server=False pattern)
736738
self.backend.server = app
739+
740+
# Skip registration if already initialized on this server
741+
# This prevents double registration when init_app() is called multiple times
742+
# (e.g., with flask run pattern where __init__ calls init_app, then user does too)
743+
if getattr(self, "_initialized_server", _UNINITIALIZED) is self.server:
744+
return
745+
self._initialized_server = self.server
746+
737747
bp_prefix = config.routes_pathname_prefix.replace("/", "_").replace(".", "_")
738748
assets_blueprint_name = f"{bp_prefix}dash_assets"
739749
self.backend.register_assets_blueprint(

0 commit comments

Comments
 (0)