From 9e5c3341965dae635e3a56f588c0837f784e1502 Mon Sep 17 00:00:00 2001 From: philippe Date: Fri, 7 Feb 2025 14:35:07 -0500 Subject: [PATCH 1/2] Fix hook priority --- dash/_hooks.py | 6 +++--- tests/integration/test_hooks.py | 16 +++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/dash/_hooks.py b/dash/_hooks.py index be1c00eee7..af3760bcb0 100644 --- a/dash/_hooks.py +++ b/dash/_hooks.py @@ -71,10 +71,10 @@ 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 + 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") From 23e753566d3e4c08a673a26ccd672263fffe8735 Mon Sep 17 00:00:00 2001 From: philippe Date: Fri, 7 Feb 2025 15:46:07 -0500 Subject: [PATCH 2/2] Add comment on priority min --- dash/_hooks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dash/_hooks.py b/dash/_hooks.py index af3760bcb0..7c4b2389f0 100644 --- a/dash/_hooks.py +++ b/dash/_hooks.py @@ -73,6 +73,7 @@ def add_hook( p = priority or 0 if not priority and len(hks): + # Take the minimum value and remove 1 to keep the order. priority_min = min(h.priority for h in hks) p = priority_min - 1