From e190768d7ae3db400e744d6d1d594969aacd2bf9 Mon Sep 17 00:00:00 2001 From: Philippe Duval Date: Tue, 31 Jul 2018 13:31:10 -0400 Subject: [PATCH] Disallow duplicate component ids. --- dash/dash.py | 25 ++++++++++++++++++------- dash/exceptions.py | 4 ++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/dash/dash.py b/dash/dash.py index 32d6ee1b22..84bad37263 100644 --- a/dash/dash.py +++ b/dash/dash.py @@ -843,13 +843,8 @@ def dispatch(self): return self.callback_map[target_id]['callback'](*args) - def _setup_server(self): - if self.config.include_assets_files: - self._walk_assets_directory() - - # Make sure `layout` is set before running the server - value = getattr(self, 'layout') - if value is None: + def _validate_layout(self): + if self.layout is None: raise exceptions.NoLayoutException( '' 'The layout was `None` ' @@ -857,6 +852,22 @@ def _setup_server(self): 'Make sure to set the `layout` attribute of your application ' 'before running the server.') + layout_id = getattr(self.layout, 'id', None) + + component_ids = {layout_id} if layout_id else set() + for component in self.layout.traverse(): + component_id = getattr(component, 'id', None) + if component_id and component_id in component_ids: + raise exceptions.DuplicateIdError( + 'Duplicate component id found : `{}`'.format(component_id)) + component_ids.add(component_id) + + def _setup_server(self): + if self.config.include_assets_files: + self._walk_assets_directory() + + self._validate_layout() + self._generate_scripts_html() self._generate_css_dist_html() diff --git a/dash/exceptions.py b/dash/exceptions.py index c459b0e5c2..58cf322715 100644 --- a/dash/exceptions.py +++ b/dash/exceptions.py @@ -50,5 +50,9 @@ class PreventUpdate(CallbackException): pass +class DuplicateIdError(DashException): + pass + + class InvalidCallbackReturnValue(CallbackException): pass