-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.py
More file actions
187 lines (154 loc) · 4.56 KB
/
app2.py
File metadata and controls
187 lines (154 loc) · 4.56 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import uuid
import dash_mantine_components as dmc
from dash import Dash, _dash_renderer, dcc, ctx, Input, Output, State, ALL
from dash.exceptions import PreventUpdate
from dash_iconify import DashIconify
_dash_renderer._set_react_version("18.2.0") # for mantine
print("------- Loading app ------ ")
# Simplified to just one list
sample_list_data = {
"title": "My Tasks",
"tasks_list": [
{
"index": uuid.uuid4().hex,
"content": "Task A",
"checked": True,
},
{
"index": uuid.uuid4().hex,
"content": "Task B",
"checked": False,
},
{
"index": uuid.uuid4().hex,
"content": "Task C",
"checked": False,
},
],
}
def get_task(task_dict):
""" Returns a single task layout """
text = task_dict["content"]
checked = task_dict["checked"]
index = task_dict["index"]
content = dmc.Grid(
[
dmc.GridCol(
dmc.Checkbox(
id={"type": "task_checked", "index": index},
checked=checked,
mt=2
),
span="content"
),
dmc.GridCol(
dmc.Text(
dcc.Input(
text,
id={"type": "task_content", "index": index},
className="shadow-input",
debounce=True
)
),
span="auto"
),
dmc.GridCol(
dmc.ActionIcon(
DashIconify(icon="tabler:x", width=20),
id={"type": "task_del", "index": index},
variant="transparent",
color="gray",
className="task-del-button"
),
span="content"
),
],
className="task-container"
)
return content
def get_tasks_layout(tasks_list):
""" Returns the list of tasks """
tasks = []
for task_dict in tasks_list:
task_layout = get_task(task_dict)
tasks.append(task_layout)
return tasks
def get_list_layout(list_data):
""" Returns the list of checkboxes """
tasks_layout = get_tasks_layout(list_data["tasks_list"])
content = dmc.Paper(
[
dmc.Title(list_data["title"], order=2),
dmc.Container(
tasks_layout,
id="main_task_container",
px=0,
mt="md",
mb="md",
),
dmc.Button(
"Add a new task",
id="new_task_button",
style={"width": "100%"},
variant="outline",
color="gray",
)
],
shadow="sm",
p="md",
mt="md",
radius="sm",
)
return content
app = Dash(__name__)
# Simplified app layout
app.layout = dmc.MantineProvider(
dmc.Container(
get_list_layout(sample_list_data),
size=400,
mt="md"
)
)
@app.callback(
Output("main_task_container", "children", allow_duplicate=True),
Input("new_task_button", "n_clicks"),
State("main_task_container", "children"),
prevent_initial_call=True,
)
def add_task(n_clicks, current_tasks):
""" Adds a task to the list """
if not n_clicks:
raise PreventUpdate
print("Entering add_task callback")
new_index = uuid.uuid4().hex
task_dict = {
"index": new_index,
"content": "",
"checked": False,
}
task_layout = get_task(task_dict)
# Add new task to current tasks
updated_tasks = current_tasks + [task_layout]
return updated_tasks
@app.callback(
Output("main_task_container", "children", allow_duplicate=True),
Input({"type": "task_del", "index": ALL}, "n_clicks"),
State("main_task_container", "children"),
prevent_initial_call=True,
)
def remove_task(n_clicks, current_tasks):
""" Remove a task from the list """
if not any(n_clicks):
raise PreventUpdate
print("Entering remove_task callback")
task_index = ctx.triggered_id["index"]
# Get the list of existing ids.
all_ids = [elem["id"] for elem in ctx.inputs_list[0]]
# Find the position of element in list and remove it
for i, task_id in enumerate(all_ids):
if task_id["index"] == task_index:
del current_tasks[i]
break
return current_tasks
if __name__ == '__main__':
app.run_server(debug=True)