Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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: 4 additions & 3 deletions dash/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ def add_hook(
return
hks = self._ns.get(hook, [])

p = 0
p = priority or 0
if not priority and len(hks):
priority_max = max(h.priority for h in hks)
p = priority_max - 1
# Take the minimum value and remove 1 to keep the order.
priority_min = min(h.priority for h in hks)
Copy link
Contributor

Choose a reason for hiding this comment

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

worth a comment in the code so the next person doesn't flip it back?

p = priority_min - 1

hks.append(_Hook(func, priority=p, data=data))
self._ns[hook] = sorted(hks, reverse=True, key=lambda h: h.priority)
Expand Down
16 changes: 9 additions & 7 deletions tests/integration/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,26 @@ def hook2(layout):
layout.children.append(html.Div("second"))
return layout

# This appears after the layout
@hooks.layout(priority=12)
def hook4(layout):
layout.children.append(html.Div("Prime"))
return layout

# Should still be last after setting a new max.
@hooks.layout()
def hook3(layout):
layout.children.append(html.Div("third"))
return layout

@hooks.layout(priority=6)
def hook4(layout):
layout.children.insert(0, html.Div("Prime"))
return layout

app = Dash()

app.layout = html.Div([html.Div("layout")], id="body")

dash_duo.start_server(app)
dash_duo.wait_for_text_to_equal("#final-wrapper > div:first-child", "final")
dash_duo.wait_for_text_to_equal("#body > div:first-child", "Prime")
dash_duo.wait_for_text_to_equal("#body > div:nth-child(2)", "layout")
dash_duo.wait_for_text_to_equal("#body > div:first-child", "layout")
dash_duo.wait_for_text_to_equal("#body > div:nth-child(2)", "Prime")
dash_duo.wait_for_text_to_equal("#body > div:nth-child(3)", "first")
dash_duo.wait_for_text_to_equal("#body > div:nth-child(4)", "second")
dash_duo.wait_for_text_to_equal("#body > div:nth-child(5)", "third")
Expand Down