Skip to content

Commit

Permalink
update todo examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Sep 17, 2024
1 parent 0b475f4 commit c51be1b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
3 changes: 2 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Examples

This directory contains some examples of the library in action, primarily through a sequence of to-do list apps of increasing complexity. For more examples of different applications and components, see the separate [examples repository](https://github.com/AnswerDotAI/fasthtml-example).
This directory contains some examples of the library in action, including a sequence of to-do list apps of increasing complexity (see `todos1.py` through `todos5.py`, along with `adv_app.py`). For more examples of different applications and components, see the separate [examples repository](https://github.com/AnswerDotAI/fasthtml-example).

33 changes: 17 additions & 16 deletions examples/first_app.py → examples/todos1.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Run with: uvicorn first_app:app --reload
from fasthtml.common import *

app,rt,todos,Todo = fast_app('data/todos.db', id=int, title=str, done=bool, pk='id')

def TodoRow(todo):
return Li(
A(todo.title, hx_get=f'/todos/{todo.id}'),
A(todo.title, href=f'/todos/{todo.id}'),
(' (done)' if todo.done else '') + ' | ',
A('edit', hx_get=f'/edit/{todo.id}'),
A('edit', href=f'/edit/{todo.id}'),
id=f'todo-{todo.id}'
)

Expand All @@ -16,14 +15,14 @@ def home():
Group(
Input(name="title", placeholder="New Todo"),
Button("Add")
), hx_post="/"
), action="/", method='post'
)
card = Card(
Ul(*map(TodoRow, todos()), id='todo-list'),
header=add,
footer=Div(id='current-todo')
)
return PageX('Todo list', card)
return Titled('Todo list', card)

@rt("/")
def get(): return home()
Expand All @@ -33,13 +32,13 @@ def post(todo:Todo):
todos.insert(todo)
return home()

@rt("/")
def put(todo: Todo):
todos.upsert(todo)
@rt("/update")
def post(todo: Todo):
todos.update(todo)
return home()

@rt("/")
def delete(id:int):
@rt("/remove")
def get(id:int):
todos.delete(id)
return home()

Expand All @@ -52,18 +51,20 @@ def get(id:int):
),
Hidden(id="id"),
CheckboxX(id="done", label='Done'),
Button('Back', hx_get='/'),
hx_put="/", id="edit"
A('Back', href='/', role="button"),
action="/update", id="edit", method='post'
)
frm = fill_form(res, todos[id])
return PageX('Edit Todo', frm)
return Titled('Edit Todo', frm)

@rt("/todos/{id}")
def get(id:int):
contents = Div(
Div(todos[id].title),
Button('Delete', hx_delete='/', value=id, name="id"),
Button('Back', hx_get='/')
A('Delete', href=f'/remove?id={id}', role="button"),
A('Back', href='/', role="button")
)
return PageX('Todo details', contents)
return Titled('Todo details', contents)

serve()

2 changes: 1 addition & 1 deletion examples/app.py → examples/todos2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Run with: python app.py
from fasthtml.common import *

id_curr = 'current-todo'
Expand Down Expand Up @@ -68,3 +67,4 @@ def get_todo(id:int):
return Div(Div(todo.title), btn)

serve()

File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ def _find_wsp(ws, data, hdrs, arg:str, p:Parameter):
if arg.lower()=='send': return partial(_send_ws, ws)
return None
res = data.get(arg, None)
if res is empty or res is None: res = hdrs.get(snake2hyphens(arg), None)
if res is empty or res is None: res = hdrs.get(arg, None)
if res is empty or res is None: res = p.default
# We can cast str and list[str] to types; otherwise just return what we have
if not isinstance(res, (list,str)) or anno is empty: return res
anno = _fix_anno(anno)
return [anno(o) for o in res] if isinstance(res,list) else anno(res)

def _wrap_ws(ws, data, params):
hdrs = data.pop('HEADERS', {})
hdrs = {k.lower().replace('-','_'):v for k,v in data.pop('HEADERS', {}).items()}
return [_find_wsp(ws, data, hdrs, arg, p) for arg,p in params.items()]

# %% ../nbs/api/00_core.ipynb
Expand Down
4 changes: 2 additions & 2 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -818,15 +818,15 @@
" if arg.lower()=='send': return partial(_send_ws, ws)\n",
" return None\n",
" res = data.get(arg, None)\n",
" if res is empty or res is None: res = hdrs.get(snake2hyphens(arg), None)\n",
" if res is empty or res is None: res = hdrs.get(arg, None)\n",
" if res is empty or res is None: res = p.default\n",
" # We can cast str and list[str] to types; otherwise just return what we have\n",
" if not isinstance(res, (list,str)) or anno is empty: return res\n",
" anno = _fix_anno(anno)\n",
" return [anno(o) for o in res] if isinstance(res,list) else anno(res)\n",
"\n",
"def _wrap_ws(ws, data, params):\n",
" hdrs = data.pop('HEADERS', {})\n",
" hdrs = {k.lower().replace('-','_'):v for k,v in data.pop('HEADERS', {}).items()}\n",
" return [_find_wsp(ws, data, hdrs, arg, p) for arg,p in params.items()]"
]
},
Expand Down

0 comments on commit c51be1b

Please sign in to comment.