Skip to content

Commit 601297d

Browse files
committed
fixes #741
1 parent 556b77b commit 601297d

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

fasthtml/core.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'RouteFuncs', 'APIRouter', 'cookie', 'reg_re_param', 'MiddlewareBase', 'FtResponse', 'unqid', 'setup_ws']
1212

1313
# %% ../nbs/api/00_core.ipynb
14-
import json,uuid,inspect,types,signal,asyncio,threading,inspect
14+
import json,uuid,inspect,types,signal,asyncio,threading,inspect,random
1515

1616
from fastcore.utils import *
1717
from fastcore.xml import *
@@ -31,7 +31,7 @@
3131
from dateutil import parser as dtparse
3232
from httpx import ASGITransport, AsyncClient
3333
from anyio import from_thread
34-
from uuid import uuid4
34+
from uuid import uuid4, UUID
3535
from base64 import b85encode,b64encode
3636

3737
from .starlette import *
@@ -810,8 +810,9 @@ def __response__(self, req):
810810
return self.cls(cts, status_code=self.status_code, headers=headers, media_type=self.media_type, background=tasks)
811811

812812
# %% ../nbs/api/00_core.ipynb
813-
def unqid():
814-
res = b64encode(uuid4().bytes)
813+
def unqid(seeded=False):
814+
id4 = UUID(int=random.getrandbits(128), version=4) if seeded else uuid4()
815+
res = b64encode(id4.bytes)
815816
return '_' + res.decode().rstrip('=').translate(str.maketrans('+/', '_-'))
816817

817818
# %% ../nbs/api/00_core.ipynb

nbs/api/00_core.ipynb

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"outputs": [],
4444
"source": [
4545
"#| export\n",
46-
"import json,uuid,inspect,types,signal,asyncio,threading,inspect\n",
46+
"import json,uuid,inspect,types,signal,asyncio,threading,inspect,random\n",
4747
"\n",
4848
"from fastcore.utils import *\n",
4949
"from fastcore.xml import *\n",
@@ -63,7 +63,7 @@
6363
"from dateutil import parser as dtparse\n",
6464
"from httpx import ASGITransport, AsyncClient\n",
6565
"from anyio import from_thread\n",
66-
"from uuid import uuid4\n",
66+
"from uuid import uuid4, UUID\n",
6767
"from base64 import b85encode,b64encode\n",
6868
"\n",
6969
"from fasthtml.starlette import *"
@@ -131,7 +131,7 @@
131131
{
132132
"data": {
133133
"text/plain": [
134-
"datetime.datetime(2025, 5, 19, 14, 0)"
134+
"datetime.datetime(2025, 5, 29, 14, 0)"
135135
]
136136
},
137137
"execution_count": null,
@@ -754,7 +754,6 @@
754754
"name": "stdout",
755755
"output_type": "stream",
756756
"text": [
757-
"b'{}'\n",
758757
"[<starlette.requests.Request object>, <starlette.applications.Starlette object>, '']\n"
759758
]
760759
}
@@ -789,7 +788,6 @@
789788
"name": "stdout",
790789
"output_type": "stream",
791790
"text": [
792-
"b'{}'\n",
793791
"Missing required field: a\n"
794792
]
795793
}
@@ -2615,15 +2613,7 @@
26152613
"execution_count": null,
26162614
"id": "73363a37",
26172615
"metadata": {},
2618-
"outputs": [
2619-
{
2620-
"name": "stdout",
2621-
"output_type": "stream",
2622-
"text": [
2623-
"b'{\"b\": \"Lorem\", \"a\": 15}'\n"
2624-
]
2625-
}
2626-
],
2616+
"outputs": [],
26272617
"source": [
26282618
"# Testing POST with Content-Type: application/json\n",
26292619
"@app.post(\"/\")\n",
@@ -2716,13 +2706,13 @@
27162706
"name": "stdout",
27172707
"output_type": "stream",
27182708
"text": [
2719-
"Set to 2025-05-19 11:46:01.972924\n"
2709+
"Set to 2025-05-29 08:31:48.235262\n"
27202710
]
27212711
},
27222712
{
27232713
"data": {
27242714
"text/plain": [
2725-
"'Session time: 2025-05-19 11:46:01.972924'"
2715+
"'Session time: 2025-05-29 08:31:48.235262'"
27262716
]
27272717
},
27282718
"execution_count": null,
@@ -3241,6 +3231,7 @@
32413231
" <html>\n",
32423232
" <head>\n",
32433233
" <title>FastHTML page</title>\n",
3234+
" <link rel=\"canonical\" href=\"http://testserver/setcookie\">\n",
32443235
" <meta charset=\"utf-8\">\n",
32453236
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, viewport-fit=cover\">\n",
32463237
"<script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/htmx.min.js\"></script><script src=\"https://cdn.jsdelivr.net/gh/answerdotai/[email protected]/fasthtml.js\"></script><script src=\"https://cdn.jsdelivr.net/gh/answerdotai/surreal@main/surreal.js\"></script><script src=\"https://cdn.jsdelivr.net/gh/gnat/css-scope-inline@main/script.js\"></script><script>\n",
@@ -3260,7 +3251,7 @@
32603251
{
32613252
"data": {
32623253
"text/plain": [
3263-
"'Cookie was set at time 11:46:35.877920'"
3254+
"'Cookie was set at time 08:31:49.013668'"
32643255
]
32653256
},
32663257
"execution_count": null,
@@ -3592,8 +3583,9 @@
35923583
"outputs": [],
35933584
"source": [
35943585
"#| export\n",
3595-
"def unqid():\n",
3596-
" res = b64encode(uuid4().bytes)\n",
3586+
"def unqid(seeded=False):\n",
3587+
" id4 = UUID(int=random.getrandbits(128), version=4) if seeded else uuid4()\n",
3588+
" res = b64encode(id4.bytes)\n",
35973589
" return '_' + res.decode().rstrip('=').translate(str.maketrans('+/', '_-'))"
35983590
]
35993591
},

0 commit comments

Comments
 (0)