-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy pathtask_list.py
105 lines (77 loc) · 2.09 KB
/
task_list.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
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "marimo",
# ]
# ///
import marimo
__generated_with = "0.8.19"
app = marimo.App()
@app.cell(hide_code=True)
def __(mo):
mo.md("# Task List").left()
return
@app.cell
def __(dataclass):
@dataclass
class Task:
name: str
done: bool = False
return (Task,)
@app.cell
def __(mo):
get_tasks, set_tasks = mo.state([])
mutation_signal, set_mutation_signal = mo.state(False)
return get_tasks, mutation_signal, set_mutation_signal, set_tasks
@app.cell
def __(mo, mutation_signal):
mutation_signal
task_entry_box = mo.ui.text(placeholder="a task ...")
return (task_entry_box,)
@app.cell
def __(Task, mo, set_mutation_signal, set_tasks, task_entry_box):
def add_task():
if task_entry_box.value:
set_tasks(lambda v: v + [Task(task_entry_box.value)])
set_mutation_signal(True)
add_task_button = mo.ui.button(
label="add task",
on_change=lambda _: add_task(),
)
clear_tasks_button = mo.ui.button(
label="clear completed tasks",
on_change=lambda _: set_tasks(
lambda v: [task for task in v if not task.done]
),
)
return add_task, add_task_button, clear_tasks_button
@app.cell
def __(add_task_button, clear_tasks_button, mo, task_entry_box):
mo.hstack(
[task_entry_box, add_task_button, clear_tasks_button], justify="start"
)
return
@app.cell
def __(Task, get_tasks, mo, set_tasks):
task_list = mo.ui.array(
[mo.ui.checkbox(value=task.done, label=task.name) for task in get_tasks()],
label="tasks",
on_change=lambda v: set_tasks(
[Task(task.name, done=v[i]) for i, task in enumerate(get_tasks())]
),
)
return (task_list,)
@app.cell
def __(mo, task_list):
mo.as_html(task_list) if task_list.value else mo.md("No tasks! 🎉")
return
@app.cell
def __():
import marimo as mo
return (mo,)
@app.cell
def __():
from dataclasses import dataclass
return (dataclass,)
if __name__ == "__main__":
app.run()