Skip to content

Commit 2e16a5f

Browse files
committed
Working background tasks on redirects
1 parent caf5167 commit 2e16a5f

File tree

1 file changed

+69
-33
lines changed

1 file changed

+69
-33
lines changed

nbs/api/00_core.ipynb

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@
131131
{
132132
"data": {
133133
"text/plain": [
134-
"datetime.datetime(2025, 5, 8, 14, 0)"
134+
"datetime.datetime(2025, 5, 12, 14, 0)"
135135
]
136136
},
137-
"execution_count": null,
137+
"execution_count": 6,
138138
"metadata": {},
139139
"output_type": "execute_result"
140140
}
@@ -155,7 +155,7 @@
155155
"True"
156156
]
157157
},
158-
"execution_count": null,
158+
"execution_count": 7,
159159
"metadata": {},
160160
"output_type": "execute_result"
161161
}
@@ -190,7 +190,7 @@
190190
"'Snake-Case'"
191191
]
192192
},
193-
"execution_count": null,
193+
"execution_count": 9,
194194
"metadata": {},
195195
"output_type": "execute_result"
196196
}
@@ -262,7 +262,7 @@
262262
"HtmxHeaders(boosted=None, current_url=None, history_restore_request=None, prompt=None, request='1', target=None, trigger_name=None, trigger=None)"
263263
]
264264
},
265-
"execution_count": null,
265+
"execution_count": 12,
266266
"metadata": {},
267267
"output_type": "execute_result"
268268
}
@@ -407,7 +407,7 @@
407407
"'HX-Trigger-After-Settle'"
408408
]
409409
},
410-
"execution_count": null,
410+
"execution_count": 21,
411411
"metadata": {},
412412
"output_type": "execute_result"
413413
}
@@ -443,7 +443,7 @@
443443
"HttpHeader(k='HX-Trigger-After-Settle', v='hi')"
444444
]
445445
},
446-
"execution_count": null,
446+
"execution_count": 23,
447447
"metadata": {},
448448
"output_type": "execute_result"
449449
}
@@ -614,7 +614,7 @@
614614
"\"['1', '2']\""
615615
]
616616
},
617-
"execution_count": null,
617+
"execution_count": 33,
618618
"metadata": {},
619619
"output_type": "execute_result"
620620
}
@@ -686,7 +686,7 @@
686686
"name": "stdout",
687687
"output_type": "stream",
688688
"text": [
689-
"[<starlette.requests.Request object>, <starlette.applications.Starlette object>, '1', HttpHeader(k='value1', v='value3')]\n"
689+
"[<starlette.requests.Request object at 0x110409d30>, <starlette.applications.Starlette object at 0x11040aae0>, '1', HttpHeader(k='value1', v='value3')]\n"
690690
]
691691
}
692692
],
@@ -712,7 +712,7 @@
712712
"name": "stdout",
713713
"output_type": "stream",
714714
"text": [
715-
"[<starlette.requests.Request object>, <starlette.applications.Starlette object>, '1', HttpHeader(k='value1', v='value3')]\n"
715+
"[<starlette.requests.Request object at 0x110430920>, <starlette.applications.Starlette object at 0x110409eb0>, '1', HttpHeader(k='value1', v='value3')]\n"
716716
]
717717
}
718718
],
@@ -754,7 +754,7 @@
754754
"name": "stdout",
755755
"output_type": "stream",
756756
"text": [
757-
"[<starlette.requests.Request object>, <starlette.applications.Starlette object>, '']\n"
757+
"[<starlette.requests.Request object at 0x1104314f0>, <starlette.applications.Starlette object at 0x11040ad20>, '']\n"
758758
]
759759
}
760760
],
@@ -1240,10 +1240,13 @@
12401240
"#| export\n",
12411241
"class Redirect:\n",
12421242
" \"Use HTMX or Starlette RedirectResponse as required to redirect to `loc`\"\n",
1243-
" def __init__(self, loc): self.loc = loc\n",
1243+
" def __init__(self, loc, background: BackgroundTask | None = None):\n",
1244+
" self.loc,self.background = loc,background\n",
12441245
" def __response__(self, req):\n",
1246+
" cts,httphdrs,tasks = _xt_cts(req, [])\n",
1247+
" if not tasks.tasks: tasks = self.background \n",
12451248
" if 'hx-request' in req.headers: return HtmxResponseHeaders(redirect=self.loc)\n",
1246-
" return RedirectResponse(self.loc, status_code=303)"
1249+
" return RedirectResponse(self.loc, status_code=303, background=tasks)"
12471250
]
12481251
},
12491252
{
@@ -1299,7 +1302,7 @@
12991302
"name": "stdout",
13001303
"output_type": "stream",
13011304
"text": [
1302-
"head-support preload class-tools loading-states multi-swap path-deps remove-me ws chunked-transfer\n"
1305+
"morph head-support preload class-tools loading-states multi-swap path-deps remove-me ws chunked-transfer\n"
13031306
]
13041307
}
13051308
],
@@ -1349,10 +1352,10 @@
13491352
{
13501353
"data": {
13511354
"text/plain": [
1352-
"'9438c74e-c727-4bdc-940a-350fafb71934'"
1355+
"'a2597a39-e99d-4460-90b9-2ca21e87bc8f'"
13531356
]
13541357
},
1355-
"execution_count": null,
1358+
"execution_count": 68,
13561359
"metadata": {},
13571360
"output_type": "execute_result"
13581361
}
@@ -1676,7 +1679,7 @@
16761679
"'f_g'"
16771680
]
16781681
},
1679-
"execution_count": null,
1682+
"execution_count": 85,
16801683
"metadata": {},
16811684
"output_type": "execute_result"
16821685
}
@@ -1738,7 +1741,7 @@
17381741
"'/foo?a=bar&b=1&b=2'"
17391742
]
17401743
},
1741-
"execution_count": null,
1744+
"execution_count": 88,
17421745
"metadata": {},
17431746
"output_type": "execute_result"
17441747
}
@@ -1763,7 +1766,7 @@
17631766
"'/foo/bar?b=1&b=2'"
17641767
]
17651768
},
1766-
"execution_count": null,
1769+
"execution_count": 89,
17671770
"metadata": {},
17681771
"output_type": "execute_result"
17691772
}
@@ -1838,7 +1841,7 @@
18381841
"'test'"
18391842
]
18401843
},
1841-
"execution_count": null,
1844+
"execution_count": 92,
18421845
"metadata": {},
18431846
"output_type": "execute_result"
18441847
}
@@ -1905,7 +1908,7 @@
19051908
"'/foo?param=value'"
19061909
]
19071910
},
1908-
"execution_count": null,
1911+
"execution_count": 95,
19091912
"metadata": {},
19101913
"output_type": "execute_result"
19111914
}
@@ -1951,7 +1954,7 @@
19511954
"'Hi there'"
19521955
]
19531956
},
1954-
"execution_count": null,
1957+
"execution_count": 97,
19551958
"metadata": {},
19561959
"output_type": "execute_result"
19571960
}
@@ -1976,7 +1979,7 @@
19761979
"'Postal'"
19771980
]
19781981
},
1979-
"execution_count": null,
1982+
"execution_count": 98,
19801983
"metadata": {},
19811984
"output_type": "execute_result"
19821985
}
@@ -2000,7 +2003,7 @@
20002003
"'testserver'"
20012004
]
20022005
},
2003-
"execution_count": null,
2006+
"execution_count": 99,
20042007
"metadata": {},
20052008
"output_type": "execute_result"
20062009
}
@@ -2055,7 +2058,7 @@
20552058
"'a yoyo'"
20562059
]
20572060
},
2058-
"execution_count": null,
2061+
"execution_count": 101,
20592062
"metadata": {},
20602063
"output_type": "execute_result"
20612064
}
@@ -2153,7 +2156,7 @@
21532156
"'Good day to you, Alexis!'"
21542157
]
21552158
},
2156-
"execution_count": null,
2159+
"execution_count": 105,
21572160
"metadata": {},
21582161
"output_type": "execute_result"
21592162
}
@@ -2200,7 +2203,7 @@
22002203
"'http://testserver/user/Alexis; http://testserver/hostie'"
22012204
]
22022205
},
2203-
"execution_count": null,
2206+
"execution_count": 107,
22042207
"metadata": {},
22052208
"output_type": "execute_result"
22062209
}
@@ -2366,7 +2369,7 @@
23662369
"'got sub/a.b'"
23672370
]
23682371
},
2369-
"execution_count": null,
2372+
"execution_count": 116,
23702373
"metadata": {},
23712374
"output_type": "execute_result"
23722375
}
@@ -2696,16 +2699,16 @@
26962699
"name": "stdout",
26972700
"output_type": "stream",
26982701
"text": [
2699-
"Set to 2025-05-08 10:13:46.558923\n"
2702+
"Set to 2025-05-12 15:48:45.492699\n"
27002703
]
27012704
},
27022705
{
27032706
"data": {
27042707
"text/plain": [
2705-
"'Session time: 2025-05-08 10:13:46.558923'"
2708+
"'Session time: 2025-05-12 15:48:45.492699'"
27062709
]
27072710
},
2708-
"execution_count": null,
2711+
"execution_count": 135,
27092712
"metadata": {},
27102713
"output_type": "execute_result"
27112714
}
@@ -3223,10 +3226,10 @@
32233226
{
32243227
"data": {
32253228
"text/plain": [
3226-
"'Cookie was set at time 10:13:47.275192'"
3229+
"'Cookie was set at time 15:48:45.745938'"
32273230
]
32283231
},
3229-
"execution_count": null,
3232+
"execution_count": 163,
32303233
"metadata": {},
32313234
"output_type": "execute_result"
32323235
}
@@ -3469,6 +3472,39 @@
34693472
"test_eq(r.status_code, 200)"
34703473
]
34713474
},
3475+
{
3476+
"cell_type": "markdown",
3477+
"id": "45274d5d",
3478+
"metadata": {},
3479+
"source": [
3480+
"Test against redirect:"
3481+
]
3482+
},
3483+
{
3484+
"cell_type": "code",
3485+
"execution_count": null,
3486+
"id": "f08266cc",
3487+
"metadata": {},
3488+
"outputs": [
3489+
{
3490+
"name": "stdout",
3491+
"output_type": "stream",
3492+
"text": [
3493+
"Starting slow task\n",
3494+
"Finished slow task\n"
3495+
]
3496+
}
3497+
],
3498+
"source": [
3499+
"@rt('/background-redirect')\n",
3500+
"def get():\n",
3501+
" return Redirect('/', background=BackgroundTask(my_slow_task))\n",
3502+
"\n",
3503+
"r = cli.get('/background-redirect', follow_redirects=False)\n",
3504+
"\n",
3505+
"test_eq(r.status_code, 303)"
3506+
]
3507+
},
34723508
{
34733509
"cell_type": "code",
34743510
"execution_count": null,

0 commit comments

Comments
 (0)