Skip to content

Commit

Permalink
Merge pull request #604 from banditburai/fix/add-default-title
Browse files Browse the repository at this point in the history
Fix/add default title
  • Loading branch information
jph00 authored Dec 22, 2024
2 parents e0ef241 + 2af4272 commit 971d401
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
6 changes: 4 additions & 2 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ def _xt_cts(req, resp):
hdr_tags = 'title','meta','link','style','base'
heads,bdy = partition(resp, lambda o: getattr(o, 'tag', '') in hdr_tags)
if resp and 'hx-request' not in req.headers and not any(getattr(o, 'tag', '')=='html' for o in resp):
resp = respond(req, heads or [Title('FastHTML page')], bdy)
title = [] if any(getattr(o, 'tag', '')=='title' for o in heads) else [Title(req.app.title)]
resp = respond(req, [*heads, *title], bdy)
return _to_xml(req, resp, indent=fh_cfg.indent), http_hdrs, ts

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

# %% ../nbs/api/00_core.ipynb
class FastHTML(Starlette):
def __init__(self, debug=False, routes=None, middleware=None, exception_handlers=None,
def __init__(self, debug=False, routes=None, middleware=None, title: str = "FastHTML page", exception_handlers=None,
on_startup=None, on_shutdown=None, lifespan=None, hdrs=None, ftrs=None, exts=None,
before=None, after=None, surreal=True, htmx=True, default_hdrs=True, sess_cls=SessionMiddleware,
secret_key=None, session_cookie='session_', max_age=365*24*3600, sess_path='/',
same_site='lax', sess_https_only=False, sess_domain=None, key_fname='.sesskey',
body_wrap=noop_body, htmlkw=None, nb_hdrs=False, **bodykw):
middleware,before,after = map(_list, (middleware,before,after))
self.title = title
hdrs,ftrs,exts = map(listify, (hdrs,ftrs,exts))
exts = {k:htmx_exts[k] for k in exts}
htmlkw = htmlkw or {}
Expand Down
61 changes: 58 additions & 3 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,8 @@
" hdr_tags = 'title','meta','link','style','base'\n",
" heads,bdy = partition(resp, lambda o: getattr(o, 'tag', '') in hdr_tags)\n",
" if resp and 'hx-request' not in req.headers and not any(getattr(o, 'tag', '')=='html' for o in resp):\n",
" resp = respond(req, heads or [Title('FastHTML page')], bdy)\n",
" title = [] if any(getattr(o, 'tag', '')=='title' for o in heads) else [Title(req.app.title)]\n",
" resp = respond(req, [*heads, *title], bdy)\n",
" return _to_xml(req, resp, indent=fh_cfg.indent), http_hdrs, ts"
]
},
Expand Down Expand Up @@ -1229,7 +1230,7 @@
{
"data": {
"text/plain": [
"'7eb16e0e-f1b9-4266-98fa-2d84c34f86fc'"
"'af1b5dfe-6a32-4348-b1de-692f979f20fb'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1351,13 +1352,14 @@
"source": [
"#| export\n",
"class FastHTML(Starlette):\n",
" def __init__(self, debug=False, routes=None, middleware=None, exception_handlers=None,\n",
" def __init__(self, debug=False, routes=None, middleware=None, title: str = \"FastHTML page\", exception_handlers=None,\n",
" on_startup=None, on_shutdown=None, lifespan=None, hdrs=None, ftrs=None, exts=None,\n",
" before=None, after=None, surreal=True, htmx=True, default_hdrs=True, sess_cls=SessionMiddleware,\n",
" secret_key=None, session_cookie='session_', max_age=365*24*3600, sess_path='/',\n",
" same_site='lax', sess_https_only=False, sess_domain=None, key_fname='.sesskey',\n",
" body_wrap=noop_body, htmlkw=None, nb_hdrs=False, **bodykw):\n",
" middleware,before,after = map(_list, (middleware,before,after))\n",
" self.title = title\n",
" hdrs,ftrs,exts = map(listify, (hdrs,ftrs,exts))\n",
" exts = {k:htmx_exts[k] for k in exts}\n",
" htmlkw = htmlkw or {}\n",
Expand Down Expand Up @@ -1666,6 +1668,59 @@
"app,cli,rt = get_cli(FastHTML(secret_key='soopersecret'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "421262a8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Route(path='/foo', name='foo', methods=['GET', 'HEAD'])]\n"
]
},
{
"data": {
"text/plain": [
"'/foo?param=value'"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"app,cli,rt = get_cli(FastHTML(title=\"My Custom Title\"))\n",
"@app.get\n",
"def foo(): return Div(\"Hello World\")\n",
"\n",
"print(app.routes)\n",
"\n",
"response = cli.get('/foo')\n",
"assert '<title>My Custom Title</title>' in response.text\n",
"\n",
"foo.to(param='value')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2ebd6270",
"metadata": {},
"outputs": [],
"source": [
"app,cli,rt = get_cli(FastHTML())\n",
"\n",
"@rt('/xt2')\n",
"def get(): return H1('bar')\n",
"\n",
"txt = cli.get('/xt2').text\n",
"assert '<title>FastHTML page</title>' in txt and '<h1>bar</h1>' in txt and '<html>' in txt"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 971d401

Please sign in to comment.