Skip to content
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

[BUG] Layout as list of components does not work when layout is a function #2905

Open
antonymilne opened this issue Jun 27, 2024 · 2 comments · May be fixed by #2915
Open

[BUG] Layout as list of components does not work when layout is a function #2905

antonymilne opened this issue Jun 27, 2024 · 2 comments · May be fixed by #2915

Comments

@antonymilne
Copy link

Describe your context

dash                       2.17.1
dash_ag_grid               31.2.0
dash-bootstrap-components  1.6.0
dash-core-components       2.0.0
dash-html-components       2.0.0
dash-mantine-components    0.12.1
dash-table                 5.0.0
dash-testing-stub          0.0.2

Describe the bug

#2795 enabled you to pass a list of components to app.layout. However, this does not work when app.layout is set to a function:

import datetime

from dash import Dash, html


def layout():
    return [html.H1(datetime.datetime.now())]


app = Dash(__name__)
app.layout = layout

app.run()

Gives:

Traceback (most recent call last):
  File "/Users/antony_milne/Library/Application Support/JetBrains/PyCharm2024.1/scratches/scratch_51.py", line 11, in <module>
    app.layout = layout
    ^^^^^^^^^^
  File "/Users/antony_milne/Library/Application Support/hatch/env/virtual/vizro/fm3ubPZu/vizro/lib/python3.11/site-packages/dash/dash.py", line 734, in layout
    [simple_clone(c) for c in layout_value._traverse_ids()],
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute '_traverse_ids'

I see that the main motivation for #2795 was that it simplified examples for beginners, so this is not such an important case because using a function for layout is already more advanced. But still I think this should probably work.

The root of the problem is that the code in Dash.layout that handles the case that layout is a function still expect it to return a single Dash component. One possible fix would be to alter this line:

layout_value = self._layout_value()

This expects a single Dash component to be returned rather than a list so it works if you just wrap it in an html.Div.

@martian0x80
Copy link
Contributor

Can I give this a try? Thanks.

@T4rk1n
Copy link
Contributor

T4rk1n commented Jul 4, 2024

Can I give this a try? Thanks.

Yes, I assigned this issue to you. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants