-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.py
213 lines (189 loc) · 7.33 KB
/
start.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
from template.dash import Dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import pandas as pd
import plotly.express as px
from database_config.engine import engine
from modules.Layout import Row, RowElement, Column, ColumnElement, Table
programs = pd.read_sql("programm", engine)
app = Dash(__name__)
app.layout = html.Div([
html.Div([
html.Img(src='assets/logo.png')
], className="header"),
html.Div([
dcc.Tabs(id="tabs", children=[
dcc.Tab(label="Análise", children=[
html.Div([
html.Div([
Column([
ColumnElement([
html.H4("Cursos")
]),
ColumnElement([
RowElement([
dcc.Dropdown(
"program-list",
[{'label': element[1], 'value': element[0]}
for element in programs.values.tolist()],
value=20
)
]),
])
],),
], id="program-list-container"),
html.Div([
Column([
ColumnElement([
html.H4("Alunos Matriculados")
]),
ColumnElement([
dcc.Dropdown("student-list",
placeholder="Selecione o estudante")
])
]),
], id="students-list-container"),
dcc.Loading(id="data-container", children=[
html.Div(id="grade-means-report-container"),
html.Div(id="extra"),
html.Div(id="courses-container"),
])
], id="data-analysis-container"),
html.Div(id="test"),
]),
dcc.Tab(label="Inserção", children=[
html.Div(id="input-data-container", children=[
html.H3("Cadastro de Estudante:"),
Row([
Column([
html.H6("Curso"),
dcc.Dropdown(
"program-list-input",
[{'label': element[1], 'value': element[0]} for element in programs.values.tolist()],
)
])
]),
Row([
RowElement([
Column([
html.H6("Nome"),
dcc.Input(id="fname", placeholder="Digite o seu nome")
]),
], 6),
RowElement([
Column([
html.H6("Matrícula"),
dcc.Input(id="student-no", placeholder="A matrícula do estudante", type="number")
]),
], 3)
]),
Row([
Column([
html.H6("Sobrenome"),
dcc.Input(id="lname", placeholder="Digite o seu sobrenome")
]),
]),
Row([
RowElement([], 9),
RowElement([
html.Button("Enviar", id="send-button")
], 0, {
'marginTop': '10px'
})
])
])
])
]),
], style={ 'padding': '0 8px' }),
html.Textarea(id="output-data")
], id="main-container")
@app.callback(
Output("student-list", "options"),
[Input("program-list", "value")]
)
def program_selected(program_id):
students = pd.read_sql_query('''
select * from students_program
where program_id={}
'''.format(program_id), engine)
students = students.drop(columns=["subject", "program_id"])
return [
{
'label': '{} {}'.format(students.iloc[i]['fname'], students.iloc[i]['lname']),
'value': students.iloc[i]['id']
} for i in range(0, len(students))
]
@app.callback(
[
Output("grade-means-report-container", "children"),
Output("courses-container", "children"),
Output("extra", "children"),
],
[Input("student-list", "value")],
prevent_initial_call=True
)
def student_selected(value):
student = pd.read_sql_query('''
select * from semester_reports
where student_id={}
'''.format(value), engine)
statistics = student.describe()
courses = student.drop(columns=[
"id", "student_id", "course_id", "first_name", "last_name", "subject", "mean"])
courses = courses.rename(columns={
'course': 'Matéria', 'semester_no': 'Semestre', 'year': 'Ano'
})
student = student.drop(columns=["id", "student_id", "course_id"])
fig = px.line(student, x=range(0, len(student)), y="mean",
title="Variação de notas do histórico escolar - {}".format(
student.iloc[0]['first_name']),
labels={'mean': 'Médias', 'x': 'Matérias feitas'}
)
# The use of dcc.Graph in the main layout causes an unrecognezed error
# on CSS grid layout. Keeping this in mind, i'll return the entire Graph
# from here
graph_layout = dcc.Graph("student-report", True, figure=fig)
courses_layout = html.Div([
html.H4("Todas as matérias feitas"),
Table(courses),
])
extra_data_layout = html.Div([
html.H4("Estatísticas"),
html.P("Nota mais baixa: {}".format(statistics['mean']['min'])),
html.P("Nota mais alta: {}".format(statistics['mean']['max'])),
html.P("Média global: {}".format(
round(statistics['mean']['mean'], 2))),
], style={
'backgroundColor': '#8395a7',
'padding': '15px 40px',
'borderRadius': '10px',
})
return graph_layout, courses_layout, extra_data_layout,
# Script injection isn't a good practice, use event listener in next commit instead
@app.callback(
Output("output-data", "value"),
[ Input("send-button", "n_clicks") ],
[
State("program-list-input", "value"),
State("student-no", "value"),
State("fname", "value"),
State("lname", "value"),
],
prevent_initial_call=True
)
def sending_forms(clicks, program, no, name, lname):
with engine.connect() as con:
query_response = con.execute('''
INSERT INTO student(fname, lname, program_id, student_no)
VALUES ('{fname}', '{lname}', {program_id}, {student_no});
'''.format(
fname=name,
lname=lname,
program_id=program,
student_no=no
))
print(query_response)
return ""
if __name__ == '__main__':
app.run_server(port=3001)