-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathtodos1.py
70 lines (59 loc) · 1.61 KB
/
todos1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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, href=f'/todos/{todo.id}'),
(' (done)' if todo.done else '') + ' | ',
A('edit', href=f'/edit/{todo.id}'),
id=f'todo-{todo.id}'
)
def home():
add = Form(
Group(
Input(name="title", placeholder="New Todo"),
Button("Add")
), action="/", method='post'
)
card = Card(
Ul(*map(TodoRow, todos()), id='todo-list'),
header=add,
footer=Div(id='current-todo')
)
return Titled('Todo list', card)
@rt("/")
def get(): return home()
@rt("/")
def post(todo:Todo):
todos.insert(todo)
return home()
@rt("/update")
def post(todo: Todo):
todos.update(todo)
return home()
@rt("/remove")
def get(id:int):
todos.delete(id)
return home()
@rt("/edit/{id}")
def get(id:int):
res = Form(
Group(
Input(id="title"),
Button("Save")
),
Hidden(id="id"),
CheckboxX(id="done", label='Done'),
A('Back', href='/', role="button"),
action="/update", id="edit", method='post'
)
frm = fill_form(res, todos[id])
return Titled('Edit Todo', frm)
@rt("/todos/{id}")
def get(id:int):
contents = Div(
Div(todos[id].title),
A('Delete', href=f'/remove?id={id}', role="button"),
A('Back', href='/', role="button")
)
return Titled('Todo details', contents)
serve()