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

Allow toasts to work with FtResponse #526

Merged
merged 1 commit into from
Oct 20, 2024
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
2 changes: 1 addition & 1 deletion fasthtml/toaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion nbs/tutorials/quickstart_for_web_devs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
},
{
Expand Down
10 changes: 10 additions & 0 deletions tests/test_toaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()

Loading