Skip to content
Open
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
7 changes: 6 additions & 1 deletion fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ def is_full_page(req, resp):
def _part_resp(req, resp):
"Partition response into HTTP headers, background tasks, and content"
resp = flat_tuple(resp)
resp = flat_tuple(o.__response__(req) if hasattr(o, '__response__') else o for o in resp)
resp = resp + tuple(getattr(req, 'injects', ()))
http_hdrs,resp = partition(resp, risinstance(HttpHeader))
tasks,resp = partition(resp, risinstance(BackgroundTask))
Expand Down Expand Up @@ -471,7 +472,11 @@ def _resp(req, resp, cls=empty, status_code=200):
if not (isinstance(cls, type) and issubclass(cls, Response)): cls=empty
if isinstance(resp, FileResponse) and not os.path.exists(resp.path): raise HTTPException(404, resp.path)
resp,kw = _part_resp(req, resp)
if isinstance(resp, Response): return resp
if isinstance(resp, Response):
if tasks := kw.get('background'):
if resp.background: tasks.tasks.insert(0, resp.background)
resp.background = tasks
return resp
if cls is not empty: return cls(resp, status_code=status_code, **kw)
if _is_ft_resp(resp):
cts = _xt_cts(req, resp)
Expand Down
20 changes: 18 additions & 2 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,7 @@
"def _part_resp(req, resp):\n",
" \"Partition response into HTTP headers, background tasks, and content\"\n",
" resp = flat_tuple(resp)\n",
" resp = flat_tuple(o.__response__(req) if hasattr(o, '__response__') else o for o in resp)\n",
" resp = resp + tuple(getattr(req, 'injects', ()))\n",
" http_hdrs,resp = partition(resp, risinstance(HttpHeader))\n",
" tasks,resp = partition(resp, risinstance(BackgroundTask))\n",
Expand Down Expand Up @@ -1546,7 +1547,11 @@
" if not (isinstance(cls, type) and issubclass(cls, Response)): cls=empty\n",
" if isinstance(resp, FileResponse) and not os.path.exists(resp.path): raise HTTPException(404, resp.path)\n",
" resp,kw = _part_resp(req, resp)\n",
" if isinstance(resp, Response): return resp\n",
" if isinstance(resp, Response):\n",
" if tasks := kw.get('background'):\n",
" if resp.background: tasks.tasks.insert(0, resp.background)\n",
" resp.background = tasks\n",
" return resp\n",
" if cls is not empty: return cls(resp, status_code=status_code, **kw)\n",
" if _is_ft_resp(resp):\n",
" cts = _xt_cts(req, resp)\n",
Expand Down Expand Up @@ -2970,7 +2975,18 @@
" print(\"Background task completed!\")\n",
" return P(\"Task started\"), BackgroundTask(long_running_task)\n",
"\n",
"response = cli.get(\"/background\")"
"response = cli.get(\"/background\")\n",
"\n",
"task_runs = []\n",
"\n",
"@app.get(\"/background-redirect\")\n",
"def background_redirect():\n",
" return Redirect(\"/\"), BackgroundTask(task_runs.append, True)\n",
"\n",
"response = cli.get(\"/background-redirect\", follow_redirects=False)\n",
"test_eq(response.status_code, 303)\n",
"test_eq(response.headers[\"location\"], \"/\")\n",
"test_eq(task_runs, [True])"
]
},
{
Expand Down