|
1085 | 1085 | " hdr_tags = 'title','meta','link','style','base'\n", |
1086 | 1086 | " heads,bdy = partition(resp, lambda o: getattr(o, 'tag', '') in hdr_tags)\n", |
1087 | 1087 | " 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", |
1089 | 1090 | " return _to_xml(req, resp, indent=fh_cfg.indent), http_hdrs, ts" |
1090 | 1091 | ] |
1091 | 1092 | }, |
|
1229 | 1230 | { |
1230 | 1231 | "data": { |
1231 | 1232 | "text/plain": [ |
1232 | | - "'7eb16e0e-f1b9-4266-98fa-2d84c34f86fc'" |
| 1233 | + "'af1b5dfe-6a32-4348-b1de-692f979f20fb'" |
1233 | 1234 | ] |
1234 | 1235 | }, |
1235 | 1236 | "execution_count": null, |
|
1351 | 1352 | "source": [ |
1352 | 1353 | "#| export\n", |
1353 | 1354 | "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", |
1355 | 1356 | " on_startup=None, on_shutdown=None, lifespan=None, hdrs=None, ftrs=None, exts=None,\n", |
1356 | 1357 | " before=None, after=None, surreal=True, htmx=True, default_hdrs=True, sess_cls=SessionMiddleware,\n", |
1357 | 1358 | " secret_key=None, session_cookie='session_', max_age=365*24*3600, sess_path='/',\n", |
1358 | 1359 | " same_site='lax', sess_https_only=False, sess_domain=None, key_fname='.sesskey',\n", |
1359 | 1360 | " body_wrap=noop_body, htmlkw=None, nb_hdrs=False, **bodykw):\n", |
1360 | 1361 | " middleware,before,after = map(_list, (middleware,before,after))\n", |
| 1362 | + " self.title = title\n", |
1361 | 1363 | " hdrs,ftrs,exts = map(listify, (hdrs,ftrs,exts))\n", |
1362 | 1364 | " exts = {k:htmx_exts[k] for k in exts}\n", |
1363 | 1365 | " htmlkw = htmlkw or {}\n", |
|
1666 | 1668 | "app,cli,rt = get_cli(FastHTML(secret_key='soopersecret'))" |
1667 | 1669 | ] |
1668 | 1670 | }, |
| 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 | + }, |
1669 | 1724 | { |
1670 | 1725 | "cell_type": "code", |
1671 | 1726 | "execution_count": null, |
|
0 commit comments