-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
404 lines (363 loc) · 15.2 KB
/
Copy pathapp.py
File metadata and controls
404 lines (363 loc) · 15.2 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
Todo App
app done with flet and sqlite and without forgetting our awesome friend python
"""
# modules
import flet
from flet import *
from datetime import datetime as dt
import sqlite3 as sql
# Okay, so the UI(User Interface) is done, now fore the final part, we can implement a database to store the tasks
# We can create a class for this
class Database:
def ConnectToDatabase():
try:
# First we should verify if the database file wheter exists or not. then make some analyses and so on connect to the database but it can be done letter
db = sql.connect('todo.db')
c = db.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS tb_tasks (
id_task INTEGER PRIMARY KEY,
task VARCHAR(255) NOT NULL,
date DATE
)
""")
return db
except Exception as e:
print(e)
def ReadDatabase(db):
c = db.cursor()
# Make sure to name the columns and not select * from ...
c.execute("SELECT * FROM tb_tasks")
records = c.fetchall()
return records
def InsertDatabase(db, values):
c = db.cursor()
# Also make sure to use ? for the inputs for security purposes
c.execute("INSERT INTO tb_tasks (task, date) VALUES (?, ?)", values)
db.commit()
def DeleteDatabase(db, value):
c = db.cursor()
# Quick note: Here we're assuming that no two task description are the same and as a result we are deleting based on task
# An ideal app would not do this but instead delete based on the actual immutable database ID. but for the sake of the tutorial and length, we will do it this way
c.execute("DELETE FROM tb_tasks WHERE task = ?", value)
db.commit()
def UpdateDatabase(db, values):
c = db.cursor()
# c.execute("UPDATE tb_tasks SET task = {t}, date = {d} WHERE task = {t}".format(t=values[2], d=values[1]))
c.execute("UPDATE tb_tasks SET task = ?, date = ? WHERE task = ?", values)
db.commit()
# Now that we have all CRUD function, we can start using the app ...
# Let's create the form class first so we can get some data
class FormContainer (UserControl):
# At this point, we can pass in a function from the main() so we can expand. Minimize the form
# Go back to the FormContainer() and add an argument as such
def __init__(self, func):
self.func = func
super().__init__()
def build(self):
return Container(
width=280,
height=80,
bgcolor="bluegrey500",
opacity=0, # change later => change this to 0 and reverse to be 1 when called.
border_radius=40,
margin=margin.only(left=-20, right=-20),
animate=animation.Animation(400, "decelarate"),
animate_opacity=200,
padding=padding.only(top=45, bottom=45),
content=Column(
horizontal_alignment=CrossAxisAlignment.CENTER,
controls=[
TextField(
height= 48,
width=255,
filled=True,
color="black",
text_size=12,
border_color="transparent",
hint_text="Description...",
hint_style=TextStyle(size=11, color="black"),
on_submit=self.func # pass the function here
),
IconButton(
content=Text("Add Task"),
width=255,
height=44,
style=ButtonStyle(
bgcolor={"":"blck"},
shape={
"": RoundedRectangleBorder(radius=8)
}
),
on_click= self.func # pass the function here
)
]
)
)
# Now, we need a class to generate a task when the user adds one
class CreateTask(UserControl):
def __init__(self, task: str, date: str, func1, func2):
# Create two instances arguments so we can pass in the delete function and edit function when we create an instance of it
self.task = task
self.date = date
self.func1 = func1
self.func2 = func2
super().__init__()
def TaskDeleteEdit(self, name, color, func):
return IconButton(
icon=name,
width=30,
icon_size=18,
icon_color=color,
opacity=0,
animate_opacity=200,
# To use it, we need to keep it in our delete and edit iconbuttons
on_click=lambda e: func(self.GetContainerInstance())
)
# We need a final thing from here, and that is the instance itself
# We need the instance identifier so that we can delete it needs to be deleted
def GetContainerInstance(self):
return self # We return the self instance
def ShowIcons(self, e):
if e.data == "true":
# these are the indexes of each icon
(
e.control.content.controls[1].controls[0].opacity,
e.control.content.controls[1].controls[1].opacity
) = (1, 1)
e.control.content.update()
else:
(
e.control.content.controls[1].controls[0].opacity,
e.control.content.controls[1].controls[1].opacity
) = (0, 0)
e.control.content.update()
def build(self):
return Container(
width=280,
height=60,
border=border.all(0.85, "white54"),
border_radius=8,
# Let's show the icons when we hover over them
on_hover=lambda e: self.ShowIcons(e),
clip_behavior=ClipBehavior.HARD_EDGE,
padding=10,
content=Row(
alignment=MainAxisAlignment.SPACE_BETWEEN,
controls=[
Column(
spacing=1,
alignment=MainAxisAlignment.CENTER,
controls=[
Text(value=self.task, size=10),
Text(value=self.date, size=9, color="white54")
]
),
# Icons delete and edit
Row(
spacing=0,
alignment=MainAxisAlignment.CENTER,
controls=[
self.TaskDeleteEdit(
icons.DELETE_ROUNDED,
"red500",
self.func1
),
self.TaskDeleteEdit(
icons.EDIT_ROUNDED,
"green500",
self.func2
),
]
)
]
)
)
def main(page: Page):
page.vertical_alignment = alignment.center
page.horizontal_alignment = alignment.center
page.bgcolor = "#cccccc"
page.title = "ToDo App done by Monji Oilíta using Flet of Python and Sqlite3"
page.name
def AddTaskToScreen(e):
# Now, everytime the user adds a task, we need to fetch the data and output it to the main column
# there are two data we need: the task and the date
#
datetime = dt.now().strftime("%b %d %Y %I:%M")
# We can use the db here for starters...
# First, open a connection to the database
db = Database.ConnectToDatabase()
Database.InsertDatabase(db, (
form.content.controls[0].value, datetime
))
db.close()
# we could also place the Database funcions within if statment...
# now recall that we set the form container to form variable. we can use this row to see if there's any content in the textfield
if form.content.controls[0].value:
_main_column_.controls.append(
# Here we can create an instance of CreateTask() class
CreateTask(
form.content.controls[0].value,
datetime,
# now the instances takes two more arguments when called...
DeleteFunction,
UpdateFunction
)
)
_main_column_.update()
# We can recall the show.hide functon for the form here
CreateToDoTask(e)
else:
db.close() #Make sure it closes even if there is no user input
def DeleteFunction(e):
# now the delete of task within the db
db = Database.ConnectToDatabase()
Database.DeleteDatabase(
db, (e.controls[0].content.controls[0].controls[0].value, )
)
# we passed it as (value,) because it needs to be a tuple data type
db.close()
# delete working
# Wehn we want to delete, recall that these instances are int the list => so that means we can simply remove them when we want to
# let's show what e is
# so the instance is passed on as e
_main_column_.controls.remove(e)
_main_column_.update()
def UpdateFunction(e):
# The update needs a little bit more work...
# We want to update from the form, so we need to pass whatever the user had from the instance back to the form, then change the functions and pass it back again
#
form.height, form.opacity = 200, 1 # Show the form
(
form.content.controls[0].value,
# Here we are changing the button function and name
# We need to change it from add task to update and so on
form.content.controls[1].content.value,
form.content.controls[1].on_click,
form.content.controls[1].on_submit
) = (
e.controls[0].content.controls[0].controls[0].value, # this is the instance value of the task
"Update",
lambda _: FinalizeUpdate(e),
lambda _: FinalizeUpdate(e),
)
form.update()
def FinalizeUpdate(e):
# Now finally, we can update the db as well
db = Database.ConnectToDatabase()
Database.UpdateDatabase(
db,
(
# first we need to get the data we want to change
form.content.controls[0].value,
dt.now().strftime("%b %d %Y %I:%M"),
# second, we insert the data to check against (the value this query should look for)
e.controls[0].content.controls[0].controls[0].value,
)
)
# we can simply reverse the values from above...
e.controls[0].content.controls[0].controls[0].value = form.content.controls[
0
].value
e.controls[0].content.update()
CreateToDoTask(e)
# Function to show/hide form container
def CreateToDoTask(e):
# when we click ADD IconButton
if form.height != 200:
form.height, form.opacity = 200, 1
form.update()
else:
form.height, form.opacity = 80, 0
# we can remove the values from the textField too ...
form.content.controls[0].value = None
form.content.controls[1].content.value = "Add Text"
form.content.controls[1].on_click = lambda e: AddTaskToScreen(e)
form.update()
_main_column_ = Column(
scroll='hidden',
expand=True,
alignment=MainAxisAlignment.START,
controls=[
Row(
alignment=MainAxisAlignment.SPACE_BETWEEN,
controls=[
Text("To-Do items", size=18, weight="bold"),
IconButton(
icons.ADD_CIRCLE_ROUNDED,
icon_size=18,
on_click= lambda e: CreateToDoTask(e)
)
]
),
Divider(height=8, color="white24")
]
)
#
page.add(
Container(
width=1500,
height=800,
margin=10,
bgcolor="bluegrey900",
alignment=alignment.center,
content=Row(
alignment=MainAxisAlignment.CENTER,
vertical_alignment=CrossAxisAlignment.CENTER,
controls=[
# main container
Container(
width=280,
height=600,
bgcolor="#0f0f0f",
border_radius=40,
border=border.all(0.5, "white"),
padding=padding.only(top=35, left=20, right=20),
clip_behavior=ClipBehavior.HARD_EDGE,
content=Column(
alignment=MainAxisAlignment.CENTER,
expand= True,
controls=[
# main column here
_main_column_,
# Form Class here
# pass in the argument for the form class here
FormContainer(lambda e: AddTaskToScreen(e))
]
)
)
]
)
)
)
page.update()
# the form container index is a follows. We can set the long element idnex as a variable so it can be called faster and easier
form = page.controls[0].content.controls[0].content.controls[1].controls[0]
# now we can call form whenever we want to do something with it
# Now to display it, we need to read the database
# Another note: Flet keeps on refreshing when we call the database functions,
# this could be from my code or from flet itself, but it should be addressed ...
# open connection
db = Database.ConnectToDatabase()
# now remember that the readdatabase() function returns the records
# Note: return is a tuple data type
# Note: we may want to display the records in reverse order, meaning the new records first followed by rhe oldest last...
# using [::-1] reverses a tuple
# using [:-1] reverses a list
for task in Database.ReadDatabase(db)[::-1]:
# let's see if the tasks are being saved
# lets add these to the screen now
_main_column_.controls.append(
# Same process as before: we create an instance of this class
CreateTask(
task[1],
task[2],
DeleteFunction,
UpdateFunction,
)
)
_main_column_.update()
if __name__ == "__main__":
flet.app(target=main)
# so other changes can be made but the foundnations are working well...