From 36f8009915ff70150fa9fea3db84ca58a370d564 Mon Sep 17 00:00:00 2001 From: Tomasz Pankowski Date: Wed, 16 Oct 2024 10:52:29 +0200 Subject: [PATCH] Allow showing toast for FtResponse --- fasthtml/toaster.py | 2 +- nbs/tutorials/quickstart_for_web_devs.ipynb | 2 +- tests/test_toaster.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/fasthtml/toaster.py b/fasthtml/toaster.py index 36b1cdce..d0ae6c16 100644 --- a/fasthtml/toaster.py +++ b/fasthtml/toaster.py @@ -51,7 +51,7 @@ def render_toasts(sess): hx_swap_oob="afterbegin:body") def toast_after(resp, req, sess): - if sk in sess and (not resp or isinstance(resp, (tuple,FT))): req.injects.append(render_toasts(sess)) + if sk in sess and (not resp or isinstance(resp, (tuple,FT,FtResponse))): req.injects.append(render_toasts(sess)) def setup_toasts(app): app.hdrs += (Style(toast_css), Script(toast_js, type="module")) diff --git a/nbs/tutorials/quickstart_for_web_devs.ipynb b/nbs/tutorials/quickstart_for_web_devs.ipynb index 1e7532a9..6f77df1b 100644 --- a/nbs/tutorials/quickstart_for_web_devs.ipynb +++ b/nbs/tutorials/quickstart_for_web_devs.ipynb @@ -1280,7 +1280,7 @@ "\n", "1. `setup_toasts` is a helper function that adds toast dependencies. Usually this would be declared right after `fast_app()`\n", "2. Toasts require sessions\n", - "3. Views with Toasts must return FT components." + "3. Views with Toasts must return FT or FtResponse components." ] }, { diff --git a/tests/test_toaster.py b/tests/test_toaster.py index 842a87cc..c3d3414a 100644 --- a/tests/test_toaster.py +++ b/tests/test_toaster.py @@ -19,6 +19,11 @@ def post(session): def get(session): return Titled("Hello, world!", P(str(session))) +@rt("/see-toast-ft-response") +def get(session): + add_toast(session, "Toast FtResponse", "info") + return FtResponse(Titled("Hello, world!", P(str(session)))) + def test_get_toaster(): cli.get('/set-toast-get', follow_redirects=False) res = cli.get('/see-toast') @@ -32,6 +37,11 @@ def test_post_toaster(): res = cli.get('/see-toast') assert 'Toast post' in res.text +def test_ft_response(): + res = cli.get('/see-toast-ft-response') + assert 'Toast FtResponse' in res.text + test_get_toaster() test_post_toaster() +test_ft_response()