diff --git a/dash/_hooks.py b/dash/_hooks.py index be1c00eee7..7c4b2389f0 100644 --- a/dash/_hooks.py +++ b/dash/_hooks.py @@ -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) + p = priority_min - 1 hks.append(_Hook(func, priority=p, data=data)) self._ns[hook] = sorted(hks, reverse=True, key=lambda h: h.priority) diff --git a/tests/integration/test_hooks.py b/tests/integration/test_hooks.py index 999819a816..ca4143eadb 100644 --- a/tests/integration/test_hooks.py +++ b/tests/integration/test_hooks.py @@ -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")