Skip to content

Commit

Permalink
fixes #387
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Aug 31, 2024
1 parent 976a018 commit c7e3258
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/basic_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get(id:int):
return fill_form(res, todos[id])

@rt("/")
def put(todo: Todo): return todos.upsert(todo), clear('current-todo')
def put(todo: Todo): return todos.update(todo), clear('current-todo')

@rt("/todos/{id}")
def get(id:int):
Expand Down
2 changes: 2 additions & 0 deletions fasthtml/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
'fasthtml.core.FastHTML': ('api/core.html#fasthtml', 'fasthtml/core.py'),
'fasthtml.core.FastHTML.__init__': ('api/core.html#fasthtml.__init__', 'fasthtml/core.py'),
'fasthtml.core.FastHTML.route': ('api/core.html#fasthtml.route', 'fasthtml/core.py'),
'fasthtml.core.FastHTML.static_route': ('api/core.html#fasthtml.static_route', 'fasthtml/core.py'),
'fasthtml.core.FastHTML.static_route_exts': ('api/core.html#fasthtml.static_route_exts', 'fasthtml/core.py'),
'fasthtml.core.FastHTML.ws': ('api/core.html#fasthtml.ws', 'fasthtml/core.py'),
'fasthtml.core.HTTPConnection.url_path_for': ( 'api/core.html#httpconnection.url_path_for',
'fasthtml/core.py'),
Expand Down
13 changes: 13 additions & 0 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,19 @@ def reg_re_param(m, s):
reg_re_param("path", ".*?")
reg_re_param("static", "ico|gif|jpg|jpeg|webm|css|js|woff|png|svg|mp4|webp|ttf|otf|eot|woff2|txt|html|map")

@patch
def static_route_exts(self:FastHTML, prefix='/', static_path='.', exts='static'):
"Add a static route at URL path `prefix` with files from `static_path` and `exts` defined by `reg_re_param()`"
@app.route(f"{prefix}{{fname:path}}.{{ext:{exts}}}")
async def get(fname:str, ext:str): return FileResponse(f'{static_path}/{fname}.{ext}')

# %% ../nbs/api/00_core.ipynb
@patch
def static_route(self:FastHTML, ext='', prefix='/', static_path='.'):
"Add a static route at URL path `prefix` with files from `static_path` and single `ext` (including the '.')"
@app.route(f"{prefix}{{fname:path}}{ext}")
async def get(fname:str): return FileResponse(f'{static_path}/{fname}{ext}')

# %% ../nbs/api/00_core.ipynb
class MiddlewareBase:
async def __call__(self, scope, receive, send) -> None:
Expand Down
50 changes: 40 additions & 10 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
{
"data": {
"text/plain": [
"datetime.datetime(2024, 8, 31, 14, 0)"
"datetime.datetime(2024, 9, 1, 14, 0)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1297,7 +1297,28 @@
"#| export\n",
"# Starlette doesn't have the '?', so it chomps the whole remaining URL\n",
"reg_re_param(\"path\", \".*?\")\n",
"reg_re_param(\"static\", \"ico|gif|jpg|jpeg|webm|css|js|woff|png|svg|mp4|webp|ttf|otf|eot|woff2|txt|html|map\")"
"reg_re_param(\"static\", \"ico|gif|jpg|jpeg|webm|css|js|woff|png|svg|mp4|webp|ttf|otf|eot|woff2|txt|html|map\")\n",
"\n",
"@patch\n",
"def static_route_exts(self:FastHTML, prefix='/', static_path='.', exts='static'):\n",
" \"Add a static route at URL path `prefix` with files from `static_path` and `exts` defined by `reg_re_param()`\"\n",
" @app.route(f\"{prefix}{{fname:path}}.{{ext:{exts}}}\")\n",
" async def get(fname:str, ext:str): return FileResponse(f'{static_path}/{fname}.{ext}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3dbef25a",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"@patch\n",
"def static_route(self:FastHTML, ext='', prefix='/', static_path='.'):\n",
" \"Add a static route at URL path `prefix` with files from `static_path` and single `ext` (including the '.')\"\n",
" @app.route(f\"{prefix}{{fname:path}}{ext}\")\n",
" async def get(fname:str): return FileResponse(f'{static_path}/{fname}{ext}')"
]
},
{
Expand Down Expand Up @@ -1661,7 +1682,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "0bfd7af6",
"id": "dd017867",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1916,7 +1937,7 @@
{
"data": {
"text/plain": [
"'Cookie was set at time 17:01:39.973522'"
"'Cookie was set at time 02:06:07.683225'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1946,13 +1967,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Set to 2024-08-30 17:01:40.008995\n"
"Set to 2024-09-01 02:06:07.701151\n"
]
},
{
"data": {
"text/plain": [
"'Session time: 2024-08-30 17:01:40.008995'"
"'Session time: 2024-09-01 02:06:07.701151'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2021,13 +2042,22 @@
{
"cell_type": "code",
"execution_count": null,
"id": "9c1863bc",
"id": "46b2e24f",
"metadata": {},
"outputs": [],
"source": [
"@rt(\"/{fname:path}.{ext:static}\")\n",
"async def get(fname:str, ext:str): return FileResponse(f'{fname}.{ext}')\n",
"\n",
"app.static_route('.md', static_path='../..')\n",
"assert 'THIS FILE WAS AUTOGENERATED' in cli.get('/README.md').text"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ed44d28",
"metadata": {},
"outputs": [],
"source": [
"app.static_route_exts()\n",
"assert 'These are the source notebooks for FastHTML' in cli.get('/README.txt').text"
]
},
Expand Down

0 comments on commit c7e3258

Please sign in to comment.