-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
302 lines (254 loc) · 12 KB
/
app.py
File metadata and controls
302 lines (254 loc) · 12 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
import flet as ft
import requests
API_BASE_URL = "http://localhost:8000/api"
def main(page: ft.Page):
page.title = "Area de Cadastro"
page.vertical_alignment = ft.MainAxisAlignment.CENTER
page.bgcolor = ft.colors.WHITE
page.margin= ft.margin.only(left=16, top=20, right=16, bottom=20)
content = ft.Column(
controls= [
ft.Text(
value='Area de navegação - logotipo - icone user - botão login e sair ',
weight=ft.FontWeight.BOLD,
color= ft.colors.AMBER,
)
]
)
#Tab - Criar aluno
nome_field = ft.TextField(label="Nome", label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
email_field = ft.TextField(label="Email",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
faixa_field = ft.TextField(label="Faixa",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
data_nascimento_field = ft.TextField(label="Data nascimento (yyyy-mm-aaa)",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
create_result = ft.Text()
container = ft.Container(height=8)
def criar_aluno_click(e):
payload = {
"nome": nome_field.value,
"email": email_field.value,
"faixa": faixa_field.value,
"data_nascimento": data_nascimento_field.value
}
response = requests.post(API_BASE_URL + '/', json=payload)
if response.status_code == 200:
aluno = response.json()
create_result.value = f'Aluno criado{aluno}'
create_result.color = "green"
else:
create_result.value = f'Error ao criar aluno{response.text}'
page.update()
create_button = ft.ElevatedButton(
text="Enviar", height=40, width=100, bgcolor=ft.colors.BLUE_600, color="white",
style=ft.ButtonStyle(shape=ft.RoundedRectangleBorder(radius=12),
padding=ft.padding.symmetric(horizontal=20, vertical=14),
overlay_color=ft.colors.BLUE_600,
elevation=4,), on_click=criar_aluno_click)
criar_tabela_aluno = ft.Column(
[ container,
nome_field,
email_field,
faixa_field,
data_nascimento_field,
create_button,
create_result
], scroll=True
)
#Tab - Listar alunos
students_table = ft.DataTable(
columns=[
ft.DataColumn(ft.Text('NOME')),
ft.DataColumn(ft.Text('EMAIL')),
ft.DataColumn(ft.Text('FAIXA')),
ft.DataColumn(ft.Text('DATA NASCIMENTO')),
],
rows=[]
)
def listar_alunos_click(e):
payload = {
"nome": nome_field.value,
"email": email_field.value,
"faixa": faixa_field.value,
"data_nascimento": data_nascimento_field.value
}
response = requests.get(API_BASE_URL + '/alunos/', json=payload)
alunos = response.json()
students_table.rows.clear()
for aluno in alunos:
row = ft.DataRow(
cells=[
ft.DataCell(ft.Text(aluno.get('nome'))),
ft.DataCell(ft.Text(aluno.get('email'))),
ft.DataCell(ft.Text(aluno.get('faixa'))),
ft.DataCell(ft.Text(aluno.get('data_nascimento'))),
]
)
students_table.rows.append(row)
list_result.value = f"{len(alunos)} alunos encontrados"
list_result.color = "green"
page.update()
list_button = ft.ElevatedButton(
text="Listar Alunos", height=40, width=200, bgcolor=ft.colors.BLUE_600, color="white",style=ft.ButtonStyle(
shape=ft.RoundedRectangleBorder(radius=12),
padding=ft.padding.symmetric(horizontal=20, vertical=14),
overlay_color=ft.colors.BLUE_600,
elevation=4,), on_click=listar_alunos_click )
container = ft.Container(height=8)
list_result = ft.Text()
listar_alunos_tab = ft.Column([container, students_table, list_button, list_result], scroll=True)
#Tab - Registrar aulas
email_aula_field = ft.TextField(label="Email Aluno",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
qtd_field = ft.TextField(label="Quantidade de aula", value=1,label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
aula_result = ft.Text()
container = ft.Container(height=8)
def marca_aula(e):
payload = {
'qtd': int(qtd_field.value),
'email_aluno': email_aula_field.value,
}
response = requests.post(API_BASE_URL + '/aula_realizada/', json=payload)
if response.status_code == 200:
aula_result.value = f"Aula cadastrada{response.json()}"
aula_result.color = "green"
else:
aula_result.value = f"Error: {response.text}"
page.update()
aula_button = ft.ElevatedButton(
text="Registrar", height=40, width=100, bgcolor=ft.colors.BLUE_600, color="white",style=ft.ButtonStyle(
shape=ft.RoundedRectangleBorder(radius=12),
padding=ft.padding.symmetric(horizontal=20, vertical=14),
overlay_color=ft.colors.BLUE_600,
elevation=4,), on_click=marca_aula)
aula_tab = ft.Column([container, email_aula_field, qtd_field, aula_button,aula_result], scroll=True)
#Tab - Progresso
email_progress_field = ft.TextField(label="Email-Aluno",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
progress_aluno_field = ft.Text()
def progress_click(e):
container = ft.Container(height=8)
email = email_progress_field.value
response = requests.get(API_BASE_URL + '/progresso_aluno/', params={'email_aluno':email})
if response.status_code == 200:
progress = response.json()
progress_aluno_field.value = (
f"Nome:{progress.get('nome')}\n"
f"Email:{progress.get('email')}\n"
f"Faixa:{progress.get('faixa')}\n"
f"Aulas:{progress.get('total_aulas')}\n"
f"Aulas Necessaria para proxima faixa:{progress.get('aulas_necessarios_para_proxima_faixa')}\n"
)
progress_aluno_field.color = "green"
else:
progress_aluno_field.value = "Error404"
page.update()
progress_button = ft.ElevatedButton(
text="Ver progresso", height=40, width=200, bgcolor=ft.colors.BLUE_600, color="white",style=ft.ButtonStyle(
shape=ft.RoundedRectangleBorder(radius=12),
padding=ft.padding.symmetric(horizontal=20, vertical=14),
overlay_color=ft.colors.BLUE_600,
elevation=4,), on_click=progress_click)
progress_tab = ft.Column([container, email_progress_field,progress_button, progress_aluno_field ], scroll=True)
#Tab - Atualizar Aluno
container = ft.Container(height=8)
id_aluno_field = ft.TextField(label="ID do Aluno",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
nome_update_field = ft.TextField(label="Novo Nome",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
email_update_field = ft.TextField(label="Novo Email",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
faixa_update_field = ft.TextField(label="Nova Faixa",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
data_nascimento_update_field = ft.TextField(label="Nova Data de Nascimento (YYYY-MM-DD)",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
update_result = ft.Text()
def atualizar_aluno_click(e):
try:
aluno_id = id_aluno_field.value
if not aluno_id:
update_result.value = "ID do aluno é necessário."
update_result.color = "red"
else:
payload = {
"nome": nome_update_field.value,
"email": email_aula_field.value,
"faixa": faixa_update_field.value,
"data_nascimento": data_nascimento_field.value,
}
response = requests.put(API_BASE_URL + f"/alunos/{aluno_id}", json=payload)
print(response)
if response.status_code == 200:
aluno = response.json()
update_result.value = f"Aluno atualizado: {aluno}"
update_result.color = "green"
else:
update_result.value = f"Erro: {response.text}"
except Exception as ex:
update_result.value = f"Exceção: {ex}"
page.update()
update_button = ft.ElevatedButton(
text="Atualizar Aluno", height=40, width=200, bgcolor=ft.colors.BLUE_600, color="white",style=ft.ButtonStyle(
shape=ft.RoundedRectangleBorder(radius=12),
padding=ft.padding.symmetric(horizontal=20, vertical=14),
overlay_color=ft.colors.BLUE_600,
elevation=4,), on_click=atualizar_aluno_click)
atualizar_tab = ft.Column(
[ container,
id_aluno_field,
nome_update_field,
email_update_field,
faixa_update_field,
data_nascimento_update_field,
update_button,
update_result,
],
scroll=True,
)
#Tab - Deletar aluno
container = ft.Container(height=8)
id_aluno_field = ft.TextField(label="ID do aluno a deletar",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
email_aluno_field = ft.TextField(label="Email",label_style= ft.TextStyle(size=12, color="black"), border_radius=10)
result_delet = ft.Text(color="red") # Exibir erros em vermelho
def delete_aluno(e):
aluno_id2 = id_aluno_field.value
email = email_aluno_field.value
if not aluno_id2 or email:
result_delet.value = "Preencha todos os campos!"
page.update()
return
try:
response = requests.delete(
f"{API_BASE_URL}/delete/{aluno_id2}",
params={"email_aluno": email}
)
if response.status_code == 200:
result_delet.value = "Usuário excluído com sucesso!"
result_delet.color = "green"
id_aluno_field.visible = False
email_aluno_field.visible = False
delet_button.visible = False
elif response.status_code == 404:
result_delet.value = "Usuário não encontrado!"
else:
result_delet.value = f"Erro ao excluir: {response.text}"
except requests.exceptions.RequestException as err:
result_delet.value = f"Erro de conexão: {err}"
page.update()
delet_button = ft.ElevatedButton(
text="Deletar", height=40, width=100, bgcolor=ft.colors.RED, color="white",style=ft.ButtonStyle(
shape=ft.RoundedRectangleBorder(radius=12),
padding=ft.padding.symmetric(horizontal=20, vertical=14),
overlay_color=ft.colors.BLUE_600,
elevation=4,), on_click=delete_aluno)
delet_tab = ft.Column([container, id_aluno_field, email_aluno_field, delet_button,result_delet], scroll=True)
#Navegação tabs
tabs = ft.Tabs(
selected_index = 0,
animation_duration= 300,
expand=True, # Faz com que as tabs ocupem toda a largura
indicator_color="blue", # Cor do indicador da aba selecionada
divider_color="transparent", # Remove a linha divisória
tabs=[
ft.Tab(text="Cadastro", icon=ft.icons.PERSON_ADD, content=criar_tabela_aluno),
ft.Tab(text="Alunos", icon=ft.icons.GROUP, content=listar_alunos_tab),
ft.Tab(text="Concluida", icon=ft.icons.CHECK_CIRCLE, content=aula_tab),
ft.Tab(text="Progresso", icon=ft.icons.TRENDING_UP, content=progress_tab),
ft.Tab(text="Atualizar", icon=ft.icons.EDIT, content=atualizar_tab),
ft.Tab(text="Deletar", icon=ft.icons.DELETE, content=delet_tab),
]
)
page.add(content,tabs)
if __name__ == "__main__":
ft.app(target=main)