-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
125 lines (111 loc) · 3.41 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from uuid import uuid4
from fastapi import FastAPI
from reactpy.backend.fastapi import configure
from reactpy import component, html, use_state
import reactpy
bootstrap_css = html.link({
"rel": "stylesheet",
"href": "https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"
})
@component
def App():
tasks, set_tasks = use_state([])
title, set_title = use_state("")
description, set_description = use_state("")
editing, set_editing = use_state(False)
task_id, set_task_id = use_state(None)
@reactpy.event(prevent_default=True)
def handle_submit(e):
if not title or not description:
return
if not editing:
new_task = {
"title": title,
"description": description,
"id": uuid4()
}
print(tasks + [new_task])
set_tasks(tasks + [new_task])
else:
print(task_id, title, description)
updated_tasks = [task if task["id"] != task_id else {
"title": title,
"description": description,
"id": task_id
} for task in tasks]
set_tasks(updated_tasks)
set_title("")
set_description("")
set_editing(False)
set_task_id(None)
def handle_delete(task_id):
print(task_id)
filtered_tasks = [task for task in tasks if task["id"] != task_id]
set_tasks(filtered_tasks)
def handle_edit(task):
print(task)
set_editing(True)
set_title(task["title"])
set_description(task["description"])
set_task_id(task["id"])
list_items = [html.li({
"key": index,
"class_name": "card card-body mb-2"
},
html.div(
html.p({
"class_name": "fw-bold h3"
}, f"{task['title']} - {task['description']}"),
html.p(
{
"class_name": "text-muted"
},
f"{task['id']}",
),
html.button({
"on_click": lambda e, task_id=task["id"]: handle_delete(task_id),
"class_name": "btn btn-danger"
}, "delete"),
html.button({
"on_click": lambda e, task=task: handle_edit(task),
"class_name": "btn btn-secondary"
}, "edit"),
)
) for index, task in enumerate(tasks)]
return html.div(
{
"style": {
"padding": "3rem",
}
},
bootstrap_css,
html.form(
{
"on_submit": handle_submit
},
html.input({
"type": "text",
"placeholder": "Title",
"on_change": lambda e: set_title(e["target"]["value"]),
"autofocus": True,
"value": title,
"class_name": "form-control mb-2"
}),
html.textarea({
"placeholder": "Description",
"on_change": lambda e: set_description(e["target"]["value"]),
"rows": 3,
"value": description,
"class_name": "form-control mb-2"
}),
html.button({
"type": "submit",
"class_name": "btn btn-primary btn-block"
}, "Create" if not editing else "Update"),
),
html.ul(
list_items
)
)
app = FastAPI()
configure(app, App)