Skip to content

Change _validate_layout to accept functions that return layouts. #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,15 @@ def _validate_layout(self):
'Make sure to set the `layout` attribute of your application '
'before running the server.')

if callable(self.layout):
to_validate = self.layout() # pylint: disable=not-callable
else:
to_validate = self.layout

layout_id = getattr(self.layout, 'id', None)

component_ids = {layout_id} if layout_id else set()
for component in self.layout.traverse():
for component in to_validate.traverse():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use self._layout_value() ? it basically does the same check.

component_id = getattr(component, 'id', None)
if component_id and component_id in component_ids:
raise exceptions.DuplicateIdError(
Expand Down
13 changes: 12 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import time

from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
from dash.exceptions import PreventUpdate, NoLayoutException
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see NoLayoutException used anywhere.

from .IntegrationTests import IntegrationTests
from .utils import assert_clean_console, invincible, wait_for

Expand Down Expand Up @@ -482,3 +482,14 @@ def test_external_files_init(self):
(("//script[@src='{}']", x) for x in js_urls),
(("//link[@href='{}']", x) for x in css_urls)):
self.driver.find_element_by_xpath(fmt.format(url))

def test_func_layout_accepted(self):

app = dash.Dash()

def create_layout():
return html.Div('Hello World')
app.layout = create_layout

self.startServer(app)
time.sleep(0.5)