Skip to content

Commit

Permalink
fixes #560
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Nov 3, 2024
1 parent e92fa1d commit 3b6c700
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 17 deletions.
1 change: 1 addition & 0 deletions fasthtml/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
'fasthtml.core.noop_body': ('api/core.html#noop_body', 'fasthtml/core.py'),
'fasthtml.core.parse_form': ('api/core.html#parse_form', 'fasthtml/core.py'),
'fasthtml.core.parsed_date': ('api/core.html#parsed_date', 'fasthtml/core.py'),
'fasthtml.core.qp': ('api/core.html#qp', 'fasthtml/core.py'),
'fasthtml.core.reg_re_param': ('api/core.html#reg_re_param', 'fasthtml/core.py'),
'fasthtml.core.respond': ('api/core.html#respond', 'fasthtml/core.py'),
'fasthtml.core.serve': ('api/core.html#serve', 'fasthtml/core.py'),
Expand Down
22 changes: 14 additions & 8 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__all__ = ['empty', 'htmx_hdrs', 'fh_cfg', 'htmx_resps', 'htmx_exts', 'htmxsrc', 'fhjsscr', 'surrsrc', 'scopesrc', 'viewport',
'charset', 'cors_allow', 'iframe_scr', 'all_meths', 'parsed_date', 'snake2hyphens', 'HtmxHeaders',
'HttpHeader', 'HtmxResponseHeaders', 'form2dict', 'parse_form', 'flat_xt', 'Beforeware', 'EventStream',
'signal_shutdown', 'uri', 'decode_uri', 'flat_tuple', 'noop_body', 'respond', 'Redirect', 'get_key',
'signal_shutdown', 'uri', 'decode_uri', 'flat_tuple', 'noop_body', 'respond', 'Redirect', 'get_key', 'qp',
'def_hdrs', 'FastHTML', 'serve', 'Client', 'APIRouter', 'cookie', 'reg_re_param', 'MiddlewareBase',
'FtResponse', 'unqid', 'setup_ws']

Expand Down Expand Up @@ -474,13 +474,10 @@ async def _f(req, exc):
return _f

# %% ../nbs/api/00_core.ipynb
def _mk_locfunc(f,p):
class _lf:
def __init__(self): update_wrapper(self, f)
def __call__(self, *args, **kw): return f(*args, **kw)
def to(self, **kw): return p + (f'?{urlencode(kw)}' if kw else '')
def __str__(self): return p
return _lf()
def qp(p:str, **kw) -> str:
"Add query parameters to path p"
kw = {k:('' if v in (False,None) else v) for k,v in kw.items()}
return p + ('?' + urlencode(kw,doseq=True) if kw else '')

# %% ../nbs/api/00_core.ipynb
def def_hdrs(htmx=True, surreal=True):
Expand Down Expand Up @@ -588,6 +585,15 @@ def ws(self:FastHTML, path:str, conn=None, disconn=None, name=None, middleware=N
def f(func=noop): return self._add_ws(func, path, conn, disconn, name=name, middleware=middleware)
return f

# %% ../nbs/api/00_core.ipynb
def _mk_locfunc(f,p):
class _lf:
def __init__(self): update_wrapper(self, f)
def __call__(self, *args, **kw): return f(*args, **kw)
def to(self, **kw): return qp(p, **kw)
def __str__(self): return p
return _lf()

# %% ../nbs/api/00_core.ipynb
@patch
def _add_route(self:FastHTML, func, path, methods, name, include_in_schema, body_wrap):
Expand Down
86 changes: 77 additions & 9 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
{
"data": {
"text/plain": [
"datetime.datetime(2024, 10, 28, 14, 0)"
"datetime.datetime(2024, 11, 4, 14, 0)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1271,18 +1271,36 @@
{
"cell_type": "code",
"execution_count": null,
"id": "00a0896c",
"id": "fde249fb",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def _mk_locfunc(f,p):\n",
" class _lf:\n",
" def __init__(self): update_wrapper(self, f)\n",
" def __call__(self, *args, **kw): return f(*args, **kw)\n",
" def to(self, **kw): return p + (f'?{urlencode(kw)}' if kw else '')\n",
" def __str__(self): return p\n",
" return _lf()"
"def qp(p:str, **kw) -> str:\n",
" \"Add query parameters to path p\"\n",
" kw = {k:('' if v in (False,None) else v) for k,v in kw.items()}\n",
" return p + ('?' + urlencode(kw,doseq=True) if kw else '')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c0836a8f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/foo?a=&b=&c=1&c=2&d=bar'"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qp('/foo', a=None, b=False, c=[1,2], d='bar')"
]
},
{
Expand Down Expand Up @@ -1447,6 +1465,23 @@
" return f"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "919618c3",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def _mk_locfunc(f,p):\n",
" class _lf:\n",
" def __init__(self): update_wrapper(self, f)\n",
" def __call__(self, *args, **kw): return f(*args, **kw)\n",
" def to(self, **kw): return qp(p, **kw)\n",
" def __str__(self): return p\n",
" return _lf()"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -1487,6 +1522,39 @@
"for o in all_meths: setattr(FastHTML, o, partialmethod(FastHTML.route, methods=o))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6ee3a86",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Route(path='/foo', name='foo', methods=['GET', 'HEAD'])]\n"
]
},
{
"data": {
"text/plain": [
"'/foo?a=bar&b=1&b=2'"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"app = FastHTML()\n",
"@app.get\n",
"def foo(a:str, b:list[int]): ...\n",
"\n",
"print(app.routes)\n",
"foo.to(a='bar', b=[1,2])"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 3b6c700

Please sign in to comment.