-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
361 lines (324 loc) · 12.3 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
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
import flet as ft
import sqlite3
def create_table() -> None:
conn = sqlite3.connect(database="student.db", check_same_thread=False)
cur = conn.cursor()
cur.execute("""
create table if not exists student(
id integer primary key autoincrement,
name varchar(25),
email varchar(50),
phone varchar(25),
address varchar(25),
francais decimal,
arabic decimal,
maths decimal,
chime decimal,
design decimal,
english decimal
)
""")
conn.commit()
cur.close()
conn.close()
create_table()
def get_data() -> list[tuple]:
con = sqlite3.connect("student.db")
cur = con.cursor()
cur.execute("select * from student"),
data = cur.fetchall()
return data
def get_sum(data: list[tuple], i: int) -> int | float:
for j in data:
if j[0] == i + 1:
return sum(j[5:])
else:
return 0
class Dialog(ft.AlertDialog):
def __init__(self, ee: sqlite3.Error) -> None:
super().__init__()
self.modal = True
self.title = ft.Text(value="WARNING !", color="#DE6751")
self.content = ft.Text(value=str(ee))
self.actions = [
ft.TextButton("Yes", on_click=self.handle_close),
ft.TextButton("No", on_click=self.handle_close),
]
def handle_close(self, e: ft.ControlEvent):
e.page.close(self)
class SelectStudent(ft.View):
def __init__(self) -> None:
super().__init__(
route="/select", scroll=ft.ScrollMode.HIDDEN, padding=ft.padding.only(left=20, right=20, bottom=20, top=30)
)
self.controls = [
ft.Container(
alignment=ft.alignment.center,
bgcolor="#57B1FD",
border_radius=ft.border_radius.all(value=25),
padding=ft.padding.all(value=10),
content=self.card(i),
)
for i in range(len(get_data()))
]
self.controls.append(
Main.button("Add Student", width=360, on_click=lambda e: e.page.go("/"))
)
self.controls.insert(0, self.__text(
value="Students", size=25, color="#DE6751", weight=ft.FontWeight.BOLD
))
@staticmethod
def __text(value: str, span: str | int | float = None, size: int = 14, **kwargs) -> ft.Text:
return ft.Text(
value=value, size=size,
spans=[
ft.TextSpan(
text=span,
style=ft.TextStyle(color=ft.colors.WHITE)
)
],
**kwargs
)
@staticmethod
def __text_result(adm) -> ft.Text:
if adm > 50:
return ft.Text(
value="Result Final: ",
weight=ft.FontWeight.BOLD,
spans=[
ft.TextSpan(
style=ft.TextStyle(color="#DE6751"),
text=f" {adm}",
),
ft.TextSpan(
text=" Acceptable 🥰",
)
]
)
else:
return ft.Text(
value="Result Final: ",
weight=ft.FontWeight.BOLD,
spans=[
ft.TextSpan(
style=ft.TextStyle(color="#DE6751"),
text=f" {adm}",
),
ft.TextSpan(
text=" unacceptable 😭",
)
]
)
def card(self, i: int) -> ft.Column:
data = get_data()
return ft.Column(
controls=[
ft.Row(
controls=[
ft.Icon(name=ft.icons.PERSON),
ft.Column(
controls=[
self.__text(value=f"Name: {data[i][1]}",
weight=ft.FontWeight.BOLD),
self.__text(value=f"Student Email: {data[i][2]}",
weight=ft.FontWeight.BOLD),
]
)
]
),
ft.Row(
controls=[
self.__text("-Phone Number: ", f"{data[i][3]}"),
self.__text("-Address: ", f"{data[i][4]}"),
]
),
ft.Row(
controls=[
self.__text("-Maths: ", f"{data[i][5]}"),
self.__text("-Arabic: ", f"{data[i][6]}"),
self.__text("-Francais: ", f"{data[i][7]}"),
]
),
ft.Row(
controls=[
self.__text("-English: ", f"{data[i][8]}"),
self.__text("-Design: ", f"{data[i][9]}"),
self.__text("-chime: ", f"{data[i][10]}"),
]
),
ft.Row(
alignment=ft.MainAxisAlignment.CENTER,
controls=[
self.__text_result(get_sum(data, i))
]
)
]
)
class Main(ft.View):
def __init__(self):
super().__init__(
route="/",
vertical_alignment=ft.MainAxisAlignment.CENTER,
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
scroll=ft.ScrollMode.AUTO,
bgcolor=ft.colors.WHITE,
padding=ft.padding.only(left=20, right=30, bottom=20, top=30)
)
self.image = ft.Image(
# src="stud2.jpg",
# src="https://th.bing.com/th/id/OIP.lMudmfiiOrnshfgTQ53yoAHaHa?pid=ImgDet&w=192&h=192&c=7",
src="https://th.bing.com/th/id/OIP.dkCi7_71hy0zKPtJ8ukkigHaGS?pid=ImgDet&w=192&h=162&c=7",
width=250,
height=200
)
self.controls = [
self.image,
ft.Text(
value="App teacher and student in your pocket".title(),
weight=ft.FontWeight.BOLD,
font_family="Calibri"
),
ft.Row(
alignment=ft.MainAxisAlignment.CENTER,
controls=[
ft.Text(
value="number of student registered: ".title(),
weight=ft.FontWeight.BOLD,
font_family="Calibri",
color="#DE6751"
),
ft.Text(
value=self.num_rows(),
weight=ft.FontWeight.BOLD,
)
]
),
ft.Container(height=10),
self.text_field(label="Student Name", icon=ft.icons.PERSON),
self.text_field(label="Email", icon=ft.icons.EMAIL),
self.text_field(label="Phone Number", icon=ft.icons.PHONE),
self.text_field(label="Address", icon=ft.icons.HOUSE),
ft.Text(
value="Marks Student",
weight=ft.FontWeight.BOLD,
size=17
),
ft.Row(
controls=[
self.text_field(label="Francais", width=103),
self.text_field(label="Arabic", width=103),
self.text_field(label="Maths", width=103)
],
alignment=ft.MainAxisAlignment.CENTER
),
ft.Row(
controls=[
self.text_field(label="Chime", width=103),
self.text_field(label="Design", width=103),
self.text_field(label="English", width=103)
],
alignment=ft.MainAxisAlignment.CENTER
),
ft.Row(
alignment=ft.MainAxisAlignment.CENTER,
controls=[
self.button(text="Add Student", on_click=self.add_student),
self.button(text="Select Student", on_click=self.select_student),
]
)
]
@staticmethod
def text_field(label: str, icon: str = None, **kwargs) -> ft.TextField:
return ft.TextField(
label=label,
icon=icon,
height=40,
cursor_color="#DE6751",
cursor_height=21,
**kwargs
)
@staticmethod
def button(text: str, width: int | float = None, **kwargs) -> ft.ElevatedButton:
return ft.ElevatedButton(
text=text,
width=width if width else 160,
style=ft.ButtonStyle(
bgcolor="#DE6751",
color=ft.colors.WHITE,
padding=ft.padding.all(value=15),
),
**kwargs
)
@staticmethod
def select_student(e: ft.ControlEvent) -> None:
e.page.go("/select")
@staticmethod
def add_student(e: ft.ControlEvent) -> None:
conn = sqlite3.connect(database="student.db", check_same_thread=False)
cur = conn.cursor()
try:
cur.execute(f"""
insert into student(
name, email, phone, address, francais, arabic, maths,
chime, design, english
) values(
'{e.control.parent.parent.controls[4].value}',
'{e.control.parent.parent.controls[5].value}',
{e.control.parent.parent.controls[6].value},
'{e.control.parent.parent.controls[7].value}',
{e.control.parent.parent.controls[9].controls[0].value},
{e.control.parent.parent.controls[9].controls[1].value},
{e.control.parent.parent.controls[9].controls[2].value},
{e.control.parent.parent.controls[10].controls[0].value},
{e.control.parent.parent.controls[10].controls[1].value},
{e.control.parent.parent.controls[10].controls[2].value}
)
""")
conn.commit()
except sqlite3.Error as ee:
e.page.open(Dialog(ee))
cur.close()
conn.close()
e.control.parent.parent.controls[2].controls[1].value = Main.num_rows()
e.control.parent.parent.controls[2].controls[1].update()
Main.clear(e)
@staticmethod
def num_rows() -> str:
conn = sqlite3.connect(database="student.db", check_same_thread=False)
n: int = conn.execute("select count(*) from student").fetchone()[0]
conn.close()
return str(n)
@staticmethod
def clear(e: ft.ControlEvent) -> None:
e.control.parent.parent.controls[4].value = ""
e.control.parent.parent.controls[5].value = ""
e.control.parent.parent.controls[6].value = ""
e.control.parent.parent.controls[7].value = ""
e.control.parent.parent.controls[9].controls[0].value = ""
e.control.parent.parent.controls[9].controls[1].value = ""
e.control.parent.parent.controls[9].controls[2].value = ""
e.control.parent.parent.controls[10].controls[0].value = ""
e.control.parent.parent.controls[10].controls[1].value = ""
e.control.parent.parent.controls[10].controls[2].value = ""
e.page.update()
def main(page: ft.Page) -> None:
page.title = "Student Note"
# page.window.top = 1
page.window.height = 760
page.window.width = 390
# page.window.left = 960
page.theme_mode = ft.ThemeMode.LIGHT
def router(route: str) -> None:
page.views.clear()
match page.route:
case "/":
page.views.append(Main())
case "/select":
page.views.append(SelectStudent())
case _:
page.launch_url("http://www.google.com")
page.update()
page.on_route_change = router
page.go("/")
page.update()
ft.app(target=main)