Skip to content

Commit 971d401

Browse files
authored
Merge pull request #604 from banditburai/fix/add-default-title
Fix/add default title
2 parents e0ef241 + 2af4272 commit 971d401

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

fasthtml/core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ def _xt_cts(req, resp):
392392
hdr_tags = 'title','meta','link','style','base'
393393
heads,bdy = partition(resp, lambda o: getattr(o, 'tag', '') in hdr_tags)
394394
if resp and 'hx-request' not in req.headers and not any(getattr(o, 'tag', '')=='html' for o in resp):
395-
resp = respond(req, heads or [Title('FastHTML page')], bdy)
395+
title = [] if any(getattr(o, 'tag', '')=='title' for o in heads) else [Title(req.app.title)]
396+
resp = respond(req, [*heads, *title], bdy)
396397
return _to_xml(req, resp, indent=fh_cfg.indent), http_hdrs, ts
397398

398399
# %% ../nbs/api/00_core.ipynb
@@ -504,13 +505,14 @@ def def_hdrs(htmx=True, surreal=True):
504505

505506
# %% ../nbs/api/00_core.ipynb
506507
class FastHTML(Starlette):
507-
def __init__(self, debug=False, routes=None, middleware=None, exception_handlers=None,
508+
def __init__(self, debug=False, routes=None, middleware=None, title: str = "FastHTML page", exception_handlers=None,
508509
on_startup=None, on_shutdown=None, lifespan=None, hdrs=None, ftrs=None, exts=None,
509510
before=None, after=None, surreal=True, htmx=True, default_hdrs=True, sess_cls=SessionMiddleware,
510511
secret_key=None, session_cookie='session_', max_age=365*24*3600, sess_path='/',
511512
same_site='lax', sess_https_only=False, sess_domain=None, key_fname='.sesskey',
512513
body_wrap=noop_body, htmlkw=None, nb_hdrs=False, **bodykw):
513514
middleware,before,after = map(_list, (middleware,before,after))
515+
self.title = title
514516
hdrs,ftrs,exts = map(listify, (hdrs,ftrs,exts))
515517
exts = {k:htmx_exts[k] for k in exts}
516518
htmlkw = htmlkw or {}

nbs/api/00_core.ipynb

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,8 @@
10851085
" hdr_tags = 'title','meta','link','style','base'\n",
10861086
" heads,bdy = partition(resp, lambda o: getattr(o, 'tag', '') in hdr_tags)\n",
10871087
" if resp and 'hx-request' not in req.headers and not any(getattr(o, 'tag', '')=='html' for o in resp):\n",
1088-
" resp = respond(req, heads or [Title('FastHTML page')], bdy)\n",
1088+
" title = [] if any(getattr(o, 'tag', '')=='title' for o in heads) else [Title(req.app.title)]\n",
1089+
" resp = respond(req, [*heads, *title], bdy)\n",
10891090
" return _to_xml(req, resp, indent=fh_cfg.indent), http_hdrs, ts"
10901091
]
10911092
},
@@ -1229,7 +1230,7 @@
12291230
{
12301231
"data": {
12311232
"text/plain": [
1232-
"'7eb16e0e-f1b9-4266-98fa-2d84c34f86fc'"
1233+
"'af1b5dfe-6a32-4348-b1de-692f979f20fb'"
12331234
]
12341235
},
12351236
"execution_count": null,
@@ -1351,13 +1352,14 @@
13511352
"source": [
13521353
"#| export\n",
13531354
"class FastHTML(Starlette):\n",
1354-
" def __init__(self, debug=False, routes=None, middleware=None, exception_handlers=None,\n",
1355+
" def __init__(self, debug=False, routes=None, middleware=None, title: str = \"FastHTML page\", exception_handlers=None,\n",
13551356
" on_startup=None, on_shutdown=None, lifespan=None, hdrs=None, ftrs=None, exts=None,\n",
13561357
" before=None, after=None, surreal=True, htmx=True, default_hdrs=True, sess_cls=SessionMiddleware,\n",
13571358
" secret_key=None, session_cookie='session_', max_age=365*24*3600, sess_path='/',\n",
13581359
" same_site='lax', sess_https_only=False, sess_domain=None, key_fname='.sesskey',\n",
13591360
" body_wrap=noop_body, htmlkw=None, nb_hdrs=False, **bodykw):\n",
13601361
" middleware,before,after = map(_list, (middleware,before,after))\n",
1362+
" self.title = title\n",
13611363
" hdrs,ftrs,exts = map(listify, (hdrs,ftrs,exts))\n",
13621364
" exts = {k:htmx_exts[k] for k in exts}\n",
13631365
" htmlkw = htmlkw or {}\n",
@@ -1666,6 +1668,59 @@
16661668
"app,cli,rt = get_cli(FastHTML(secret_key='soopersecret'))"
16671669
]
16681670
},
1671+
{
1672+
"cell_type": "code",
1673+
"execution_count": null,
1674+
"id": "421262a8",
1675+
"metadata": {},
1676+
"outputs": [
1677+
{
1678+
"name": "stdout",
1679+
"output_type": "stream",
1680+
"text": [
1681+
"[Route(path='/foo', name='foo', methods=['GET', 'HEAD'])]\n"
1682+
]
1683+
},
1684+
{
1685+
"data": {
1686+
"text/plain": [
1687+
"'/foo?param=value'"
1688+
]
1689+
},
1690+
"execution_count": null,
1691+
"metadata": {},
1692+
"output_type": "execute_result"
1693+
}
1694+
],
1695+
"source": [
1696+
"app,cli,rt = get_cli(FastHTML(title=\"My Custom Title\"))\n",
1697+
"@app.get\n",
1698+
"def foo(): return Div(\"Hello World\")\n",
1699+
"\n",
1700+
"print(app.routes)\n",
1701+
"\n",
1702+
"response = cli.get('/foo')\n",
1703+
"assert '<title>My Custom Title</title>' in response.text\n",
1704+
"\n",
1705+
"foo.to(param='value')"
1706+
]
1707+
},
1708+
{
1709+
"cell_type": "code",
1710+
"execution_count": null,
1711+
"id": "2ebd6270",
1712+
"metadata": {},
1713+
"outputs": [],
1714+
"source": [
1715+
"app,cli,rt = get_cli(FastHTML())\n",
1716+
"\n",
1717+
"@rt('/xt2')\n",
1718+
"def get(): return H1('bar')\n",
1719+
"\n",
1720+
"txt = cli.get('/xt2').text\n",
1721+
"assert '<title>FastHTML page</title>' in txt and '<h1>bar</h1>' in txt and '<html>' in txt"
1722+
]
1723+
},
16691724
{
16701725
"cell_type": "code",
16711726
"execution_count": null,

0 commit comments

Comments
 (0)