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
5 changes: 2 additions & 3 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,13 +775,12 @@ async def __call__(self, scope, receive, send) -> None:
# %% ../nbs/api/00_core.ipynb
class FtResponse:
"Wrap an FT response with any Starlette `Response`"
def __init__(self, content, status_code:int=200, headers=None, cls=HTMLResponse, media_type:str|None=None, background: BackgroundTask | None = None):
def __init__(self, content, status_code:int=200, headers=None, cls=HTMLResponse, media_type:str|None=None):
self.content,self.status_code,self.headers = content,status_code,headers
self.cls,self.media_type,self.background = cls,media_type,background
self.cls,self.media_type = cls,media_type

def __response__(self, req):
cts,httphdrs,tasks = _xt_cts(req, self.content)
if not tasks.tasks: tasks = self.background
headers = {**(self.headers or {}), **httphdrs}
return self.cls(cts, status_code=self.status_code, headers=headers, media_type=self.media_type, background=tasks)

Expand Down
97 changes: 7 additions & 90 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, 3, 20, 14, 0)"
"datetime.datetime(2025, 3, 16, 14, 0)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1231,7 +1231,7 @@
{
"data": {
"text/plain": [
"'a2597a39-e99d-4460-90b9-2ca21e87bc8f'"
"'cc87253c-bfc1-4544-bbc0-58dd8d3291bc'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2578,13 +2578,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Set to 2025-03-20 09:00:21.844393\n"
"Set to 2025-03-16 04:41:09.011712\n"
]
},
{
"data": {
"text/plain": [
"'Session time: 2025-03-20 09:00:21.844393'"
"'Session time: 2025-03-16 04:41:09.011712'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -3105,7 +3105,7 @@
{
"data": {
"text/plain": [
"'Cookie was set at time 09:00:22.051066'"
"'Cookie was set at time 04:41:09.620176'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -3236,13 +3236,12 @@
"#| export\n",
"class FtResponse:\n",
" \"Wrap an FT response with any Starlette `Response`\"\n",
" def __init__(self, content, status_code:int=200, headers=None, cls=HTMLResponse, media_type:str|None=None, background: BackgroundTask | None = None):\n",
" def __init__(self, content, status_code:int=200, headers=None, cls=HTMLResponse, media_type:str|None=None):\n",
" self.content,self.status_code,self.headers = content,status_code,headers\n",
" self.cls,self.media_type,self.background = cls,media_type,background\n",
" self.cls,self.media_type = cls,media_type\n",
"\n",
" def __response__(self, req):\n",
" cts,httphdrs,tasks = _xt_cts(req, self.content)\n",
" if not tasks.tasks: tasks = self.background\n",
" headers = {**(self.headers or {}), **httphdrs}\n",
" return self.cls(cts, status_code=self.status_code, headers=headers, media_type=self.media_type, background=tasks)"
]
Expand All @@ -3267,88 +3266,6 @@
"assert '<title>Foo</title>' in txt and '<h1>bar</h1>' in txt and '<html>' in txt"
]
},
{
"cell_type": "markdown",
"id": "3f506103",
"metadata": {},
"source": [
"Test on a single background task:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ea66093",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Starting slow task\n",
"Finished slow task\n"
]
}
],
"source": [
"def my_slow_task():\n",
" print('Starting slow task') \n",
" time.sleep(3)\n",
" print('Finished slow task') \n",
"\n",
"@rt('/background')\n",
"def get():\n",
" return FtResponse(P('BG Task'), background=BackgroundTask(my_slow_task))\n",
"\n",
"r = cli.get('/background')\n",
"\n",
"test_eq(r.status_code, 200)\n"
]
},
{
"cell_type": "markdown",
"id": "881a88bb",
"metadata": {},
"source": [
"Test multiple background tasks:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1764a6aa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sleeping for 0s\n",
"Slept for 0s\n",
"Sleeping for 1s\n",
"Slept for 1s\n",
"Sleeping for 2s\n",
"Slept for 2s\n"
]
}
],
"source": [
"def increment(amount):\n",
" print(f'Sleeping for {amount}s') \n",
" time.sleep(amount)\n",
" print(f'Slept for {amount}s') \n",
"\n",
"@rt('/backgrounds')\n",
"def get():\n",
" tasks = BackgroundTasks()\n",
" for i in range(3): tasks.add_task(increment, i)\n",
" return FtResponse(P('BG Tasks'), background=tasks)\n",
"\n",
"r = cli.get('/backgrounds')\n",
"\n",
"test_eq(r.status_code, 200)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
Loading