Skip to content

Commit 3f2a824

Browse files
authored
Merge pull request #778 from AnswerDotAI/erikgaas/fix_toasts776
Correct afterware resp objects
2 parents 2396ee8 + 8828c90 commit 3f2a824

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

fasthtml/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ async def _f(req):
620620
if not resp: resp = await _wrap_call(f, req, sig.parameters)
621621
for a in self.after:
622622
wreq = await _wrap_req(req, _params(a))
623+
wreq['resp'] = resp
623624
nr = a(**wreq)
624625
if nr: resp = nr
625626
return _resp(req, resp, sig.return_annotation)

nbs/api/00_core.ipynb

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
{
132132
"data": {
133133
"text/plain": [
134-
"datetime.datetime(2025, 9, 12, 14, 0)"
134+
"datetime.datetime(2025, 9, 11, 14, 0)"
135135
]
136136
},
137137
"execution_count": null,
@@ -1725,6 +1725,7 @@
17251725
" if not resp: resp = await _wrap_call(f, req, sig.parameters)\n",
17261726
" for a in self.after:\n",
17271727
" wreq = await _wrap_req(req, _params(a))\n",
1728+
" wreq['resp'] = resp\n",
17281729
" nr = a(**wreq)\n",
17291730
" if nr: resp = nr\n",
17301731
" return _resp(req, resp, sig.return_annotation)\n",
@@ -2878,13 +2879,13 @@
28782879
"name": "stdout",
28792880
"output_type": "stream",
28802881
"text": [
2881-
"Set to 2025-09-12 08:12:01.314167\n"
2882+
"Set to 2025-09-11 16:36:20.106162\n"
28822883
]
28832884
},
28842885
{
28852886
"data": {
28862887
"text/plain": [
2887-
"'Session time: 2025-09-12 08:12:01.314167'"
2888+
"'Session time: 2025-09-11 16:36:20.106162'"
28882889
]
28892890
},
28902891
"execution_count": null,
@@ -3081,15 +3082,37 @@
30813082
"outputs": [],
30823083
"source": [
30833084
"app,cli,rt = get_cli(FastHTML())\n",
3084-
"setup_toasts(app)\n",
3085+
"\n",
3086+
"# Modified toast functions to test resp logic\n",
3087+
"def toast_after_(resp, req, sess):\n",
3088+
" if sk in sess and (not resp or isinstance(resp, (tuple,FT,FtResponse))):\n",
3089+
" sess['toast_duration'] = req.app.state.toast_duration\n",
3090+
" # We will activate the toast depending on the content of the response\n",
3091+
" if str('love') in str(resp): req.injects.append(render_toasts(sess))\n",
3092+
"\n",
3093+
"def setup_toasts_(app, duration=5000):\n",
3094+
" app.state.toast_duration = duration\n",
3095+
" app.hdrs += [Style(toast_css), Script(js)]\n",
3096+
" app.after.append(toast_after_)\n",
3097+
"\n",
3098+
"setup_toasts_(app)\n",
3099+
"\n",
3100+
"tm = 'Here is a toast!'\n",
30853101
"\n",
30863102
"@rt(\"/\")\n",
30873103
"def index(sess): \n",
3088-
" add_toast(sess, \"Successful Page Load!\", \"success\")\n",
3104+
" add_toast(sess, tm, \"success\")\n",
30893105
" return P(\"I love toast!\")\n",
30903106
"\n",
3107+
"@rt(\"/no-toast\")\n",
3108+
"def no_toast(sess):\n",
3109+
" add_toast(sess, tm, \"success\")\n",
3110+
" return P(\"I hate toast!\")\n",
3111+
"\n",
30913112
"r = cli.get('/')\n",
3092-
"assert 'Successful Page Load!' in r.text"
3113+
"assert tm in r.text\n",
3114+
"r = cli.get('/no-toast')\n",
3115+
"assert not tm in r.text"
30933116
]
30943117
},
30953118
{

0 commit comments

Comments
 (0)