-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
320 lines (278 loc) · 8.52 KB
/
app.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
# -*- coding: utf-8 -*-
import datetime
from datetime import date
import dash
import plotly.graph_objs as go
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import plotly.express as px
# import calendar
import plotly.express as px
import pandas as pd
import dash_table
import pandas as pd
# the style arguments for the main content page.
CONTENT_STYLE = {
'margin-left': '15%',
'margin-right': '15%',
'top': 0,
'padding': '20px 10px'
}
TEXT_STYLE = {
'textAlign': 'center',
'color': '#191970'
}
CARD_TEXT_STYLE = {
'textAlign': 'center',
'color': '#0074D9'
}
ALERT_STYLE = {
'width' : '100%'
}
# Cargamos la base
df = pd.read_csv('base.csv')
# Convertimos el campo impacto_presupuestario_mes a fecha
df['impacto_presupuestario_mes'] = pd.to_datetime(df['impacto_presupuestario_mes'], format='%m').dt.month
# Rango del eje X de los gráficos
range_x1 = df['impacto_presupuestario_mes'].iloc[0]
range_x2 = df['impacto_presupuestario_mes'].iloc[-1]
# Filtramos incisos
df = df.loc[df['inciso_id']<6]
# Obtención de la ejecucion, ultima actualizacion, y SAF
vigente = df['credito_vigente'].sum
devengado = df['credito_devengado'].sum
ejecucion = ((devengado(0)/vigente(0)).round(2))*100
ejecucion = str(round(ejecucion, 2))
ejecucion_texto = "Ejecución: "+ str(ejecucion) + "%"
actualizacion = df['ultima_actualizacion_fecha'].iloc[0]
saf_id = df['servicio_id'].iloc[0]
titulo = "Ejecución Presupuestaria SAF " + str(saf_id)
programas = df['programa_desc'].unique()
df.rename(columns = {'credito_devengado': 'Credito Devengado',
'impacto_presupuestario_mes': 'Mes',
'fuente_financiamiento_desc' : 'Fuente de Financiamiento',
'programa_desc' : 'Programa'
},
inplace = True)
# -------------------------------------------------------
# APP DE DASH
# -------------------------------------------------------
app = dash.Dash(__name__,
external_stylesheets=[dbc.themes.MATERIA],
meta_tags=[{'name': 'viewport',
# "content": "width=device-width, initial-scale=1"
'content': 'width=device-width, initial-scale=1.0, maximum-scale=1.2, minimum-scale=0.5,'
}]
)
server = app.server
alert = dbc.Alert("Por favor seleccione un programa presupuestario. ",
color="danger",
duration = 3000,
)
# Grafico estatico
table= df.groupby(['Mes','Fuente de Financiamiento'])['Credito Devengado'].sum().unstack()
table = table.assign(TOTAL=table.sum(1)).stack().to_frame('Credito Devengado')
table.reset_index(inplace=True)
saf = px.line(table,
x='Mes',
y='Credito Devengado',
color = 'Fuente de Financiamiento',
template = 'plotly_white'
# template = 'plotly_dark',
)
saf.update_layout(
legend=dict(
x=0.01,
y=0.95,
traceorder="normal",
font=dict(
# family="sans-serif",
# size=10,
color="black"
),
),
xaxis=dict(
tickmode='linear',
tick0=1,
dtick=1
)
)
saf.update_xaxes(range=[range_x1, range_x2])
content_alert_row = dbc.Row([
html.Div(
id="alert_prg",
children=[],
style=ALERT_STYLE
)
])
content_info_row = dbc.Row([
dbc.Col(
dbc.Card(
[
dbc.CardBody(
[
html.H4(id='card_title_1', children=[ejecucion_texto], className='card-title',
style=CARD_TEXT_STYLE),
# html.P(id='card_text_1', children=['Ejecución a la fecha'], style=CARD_TEXT_STYLE),
]
)
]
),
md=6
),
dbc.Col(
dbc.Card(
[
dbc.CardBody(
[
html.H4(actualizacion, className='card-title', style=CARD_TEXT_STYLE),
# html.P('Última actualización.', style=CARD_TEXT_STYLE),
]
),
]
),
md=6
),
])
content_select_row = dbc.Row([
dbc.Col(
dbc.Card(
[
dbc.CardBody(
[dcc.Dropdown(
id='prg-dpdn',
options=[{'label': x.title(), 'value': x} for x in sorted(programas)],
value=[df['Programa'].iloc[0]],
placeholder="Seleccione programa presupuestario ",
# bs_size="sm",
multi=True,
style=dict(
width='100%',
display='inline-block',
# verticalAlign="middle",
# fontSize=10,
# height="100%",
),
),
]
)
]
),
md=12
),
])
content_third_row = dbc.Row(
[
dbc.Col(
dcc.Graph(id='grapsaf-graph', figure = saf), md=12
),
]
)
content_fourth_row = dbc.Row(
[
dbc.Col(
dcc.Graph(id='prg-graph'), md=12
),
]
)
content_fifth_row = dbc.Row(
[
dbc.Col(dash_table.DataTable(
id='prg-tbl',
columns=[
{'name': 'Programa', 'id': 'Programa'},
{'name': 'Credito Vigente', 'id': 'credito_vigente'},
{'name': 'Credito Devengado', 'id': 'Credito Devengado'},
],
page_action="native",
page_size=10,
style_as_list_view=True,
style_cell={
'padding': '1px',
},
),
)
]
)
content_footer_row = dbc.Row([
dbc.Col(
html.A('Fuente: Presupuesto Abierto', href='http://www.presupuestoabierto.gob.ar', target="_blank")
),
dbc.Col(
),
dbc.Col(
html.A('By Mato', href='http://matog.github.io/cv', target="_blank"), style={'text-align': 'right'}
)
])
app.title = 'Ejecución Presupuestaria'
content = html.Div(
[
html.H2('Monitor de Ejecución presupuestaria SAF364', style=TEXT_STYLE),
html.Hr(),
content_alert_row,
html.Br(),
content_info_row,
html.Br(),
# content_text_info_row,
content_select_row,
html.Br(),
content_third_row,
content_fourth_row,
html.Br(),
content_fifth_row,
html.Br(),
content_footer_row
],
style=CONTENT_STYLE
)
app.layout = html.Div([content])
# ---------------------------------------------------------------
# Callback
# ---------------------------------------------------------------
# Graficamos los programas
@app.callback(
Output("alert_prg", "children"),
Output("prg-graph", 'figure'),
Output("prg-tbl", 'data'),
Input('prg-dpdn', 'value'),
)
def programas(programa):
if len(programa) > 0:
dff = df[df.Programa.isin(programa)]
dff = dff.groupby(['Mes','Programa']).sum().reset_index()
fig = px.line(dff,
x='Mes',
y='Credito Devengado',
color='Programa',
template = 'plotly_white'
)
fig.update_layout(
legend=dict(
x=0.01,
y=0.95,
traceorder="normal",
font=dict(
# family="sans-serif",
# size=10,
color="black"
),
),
xaxis=dict(
tickmode='linear',
tick0=1,
dtick=1
)
)
fig.update_xaxes(range=[range_x1, range_x2])
dff_tbl = df[df.Programa.isin(programa)]
dff_tbl = dff_tbl.groupby(['Programa'])['credito_vigente', 'Credito Devengado'].sum().reset_index()
dff_tbl = dff_tbl.round(3)
# print(dff_tbl)
return dash.no_update, fig, dff_tbl.to_dict(orient='records')
elif len(programa) == 0:
return alert, dash.no_update, dash.no_update
if __name__ == '__main__':
app.run_server(debug=True, host='0.0.0.0', port=8050)