Skip to content
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
1 change: 1 addition & 0 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ async def _f(req):
if not resp: resp = await _wrap_call(f, req, sig.parameters)
for a in self.after:
wreq = await _wrap_req(req, _params(a))
wreq['resp'] = resp
nr = a(**wreq)
if nr: resp = nr
return _resp(req, resp, sig.return_annotation)
Expand Down
35 changes: 29 additions & 6 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
{
"data": {
"text/plain": [
"datetime.datetime(2025, 9, 12, 14, 0)"
"datetime.datetime(2025, 9, 11, 14, 0)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1725,6 +1725,7 @@
" if not resp: resp = await _wrap_call(f, req, sig.parameters)\n",
" for a in self.after:\n",
" wreq = await _wrap_req(req, _params(a))\n",
" wreq['resp'] = resp\n",
" nr = a(**wreq)\n",
" if nr: resp = nr\n",
" return _resp(req, resp, sig.return_annotation)\n",
Expand Down Expand Up @@ -2878,13 +2879,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Set to 2025-09-12 08:12:01.314167\n"
"Set to 2025-09-11 16:36:20.106162\n"
]
},
{
"data": {
"text/plain": [
"'Session time: 2025-09-12 08:12:01.314167'"
"'Session time: 2025-09-11 16:36:20.106162'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -3081,15 +3082,37 @@
"outputs": [],
"source": [
"app,cli,rt = get_cli(FastHTML())\n",
"setup_toasts(app)\n",
"\n",
"# Modified toast functions to test resp logic\n",
"def toast_after_(resp, req, sess):\n",
" if sk in sess and (not resp or isinstance(resp, (tuple,FT,FtResponse))):\n",
" sess['toast_duration'] = req.app.state.toast_duration\n",
" # We will activate the toast depending on the content of the response\n",
" if str('love') in str(resp): req.injects.append(render_toasts(sess))\n",
"\n",
"def setup_toasts_(app, duration=5000):\n",
" app.state.toast_duration = duration\n",
" app.hdrs += [Style(toast_css), Script(js)]\n",
" app.after.append(toast_after_)\n",
"\n",
"setup_toasts_(app)\n",
"\n",
"tm = 'Here is a toast!'\n",
"\n",
"@rt(\"/\")\n",
"def index(sess): \n",
" add_toast(sess, \"Successful Page Load!\", \"success\")\n",
" add_toast(sess, tm, \"success\")\n",
" return P(\"I love toast!\")\n",
"\n",
"@rt(\"/no-toast\")\n",
"def no_toast(sess):\n",
" add_toast(sess, tm, \"success\")\n",
" return P(\"I hate toast!\")\n",
"\n",
"r = cli.get('/')\n",
"assert 'Successful Page Load!' in r.text"
"assert tm in r.text\n",
"r = cli.get('/no-toast')\n",
"assert not tm in r.text"
]
},
{
Expand Down