Proof4146 commited on
Commit
0ef5949
·
verified ·
1 Parent(s): a96d8fe

Upload 3 files

Browse files
Files changed (3) hide show
  1. front.py +177 -0
  2. requirements.txt +5 -3
  3. runtime.txt +1 -0
front.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+ import plotly.express as px
5
+
6
+
7
+ st.set_page_config(layout="wide")
8
+
9
+ st.markdown("""
10
+ <style>
11
+ .block-container {
12
+ padding-top: 1rem;
13
+ padding-bottom: 0rem;
14
+ padding-left: 5rem;
15
+ padding-right: 5rem;
16
+ max-width: 100%;
17
+ }
18
+
19
+ .centered-header {
20
+ text-align: center;
21
+ font-size: 24px;
22
+ font-weight: bold;
23
+ margin-bottom: 10px;
24
+ color: #FFC1C7;
25
+ }
26
+ </style>
27
+ """, unsafe_allow_html=True)
28
+
29
+
30
+ API_URL = "https://9ls1vcbcof.execute-api.us-east-1.amazonaws.com/prueba1-apii/base"
31
+
32
+ def obtener_datos_cliente(documento):
33
+ doc_api = f"{documento}_"
34
+ params = {"documento": doc_api}
35
+ try:
36
+ response = requests.get(API_URL, params=params)
37
+ response.raise_for_status()
38
+ data = response.json()
39
+ items = data.get("items", [])
40
+ return pd.DataFrame(items)
41
+ except Exception as e:
42
+ st.error(f"Error al consultar la API: {e}")
43
+ return pd.DataFrame()
44
+
45
+ # ========================
46
+ def mostrar_kpi(titulo, valor, color_base="#87CEEB"):
47
+ color_interno = "#D0F0FF"
48
+ color_borde = "#00BFFF"
49
+ st.markdown(
50
+ f"""
51
+ <div style="
52
+ background-color: {color_interno};
53
+ border: 2px solid {color_borde};
54
+ padding: 15px;
55
+ border-radius: 12px;
56
+ text-align: center;
57
+ margin-bottom: 10px;
58
+ box-shadow: 2px 2px 5px rgba(0,0,0,0.15);
59
+ font-size: 18px;
60
+ font-weight: bold;
61
+ color: #000080;
62
+ ">
63
+ {titulo}<br><span style="font-size:22px;">{valor}</span>
64
+ </div>
65
+ """,
66
+ unsafe_allow_html=True
67
+ )
68
+
69
+ # interfaz streamlit
70
+ st.title("Consulta de Datos de Cliente")
71
+
72
+ doc_input = st.text_input("Ingrese el documento del cliente:", value="-2747430000000000000")
73
+
74
+ if st.button("Buscar"):
75
+ if doc_input.strip() == "":
76
+ st.warning("Ingrese un documento válido.")
77
+ else:
78
+ df = obtener_datos_cliente(doc_input)
79
+ if df.empty:
80
+ st.info("No se encontraron datos para este documento.")
81
+ else:
82
+ st.subheader(f"Datos del cliente {doc_input}")
83
+
84
+ # Primera fila: tabla de datos y KPIs a la derecha
85
+ col_tabla, col_kpis = st.columns([0.7, 0.3])
86
+
87
+ with col_tabla:
88
+ st.dataframe(df)
89
+
90
+ with col_kpis:
91
+ categoria_mayor_gasto = df.loc[df["mnt_trx_mm"].idxmax(), "categoria"]
92
+ mostrar_kpi("Categoría con mayor gasto", categoria_mayor_gasto)
93
+
94
+ categoria_mas_trx = df.loc[df["num_trx"].idxmax(), "categoria"]
95
+ mostrar_kpi("Categoría con más transacciones", categoria_mas_trx)
96
+
97
+ col_bar1, col_bar2 = st.columns(2)
98
+
99
+ with col_bar1:
100
+ st.markdown('<div class="centered-header">Monto por Categoría</div>', unsafe_allow_html=True)
101
+
102
+ # monto por categora
103
+ df_monto = df.sort_values("mnt_trx_mm", ascending=False)
104
+ fig_monto = px.bar(
105
+ df_monto,
106
+ x="mnt_trx_mm",
107
+ y="categoria",
108
+ orientation="h",
109
+ text="mnt_trx_mm",
110
+ hover_data={
111
+ "categoria": True,
112
+ "mnt_trx_mm": True,
113
+ "num_trx": True,
114
+ "pct_mnt_tot": True,
115
+ "pct_num_trx_tot": True
116
+ },
117
+ labels={"mnt_trx_mm": "Monto", "num_trx": "Número de transacciones"},
118
+ text_auto=True
119
+ )
120
+ fig_monto.update_traces(textfont_color='darkblue', marker_color="#a1cbe7")
121
+ fig_monto.update_layout(
122
+ height=500,
123
+ margin=dict(l=50, r=50, t=20, b=50),
124
+ showlegend=False,
125
+ xaxis_title="Monto",
126
+ yaxis_title="Categoría"
127
+ )
128
+ st.plotly_chart(fig_monto, use_container_width=True)
129
+
130
+ with col_bar2:
131
+ st.markdown('<div class="centered-header">Número de Transacciones por Categoría</div>', unsafe_allow_html=True)
132
+
133
+ # num de transacciones por categoria
134
+ df_num = df.sort_values("num_trx", ascending=False)
135
+ fig_num = px.bar(
136
+ df_num,
137
+ x="num_trx",
138
+ y="categoria",
139
+ orientation="h",
140
+ text="num_trx",
141
+ hover_data={
142
+ "categoria": True,
143
+ "mnt_trx_mm": True,
144
+ "num_trx": True,
145
+ "pct_mnt_tot": True,
146
+ "pct_num_trx_tot": True
147
+ },
148
+ labels={"num_trx": "Número de transacciones", "mnt_trx_mm": "Monto"},
149
+ text_auto=True
150
+ )
151
+ fig_num.update_traces(textfont_color='darkgreen', marker_color="#89ffa7")
152
+ fig_num.update_layout(
153
+ height=500,
154
+ margin=dict(l=50, r=50, t=20, b=50),
155
+ showlegend=False,
156
+ xaxis_title="Número de Transacciones",
157
+ yaxis_title="Categoría"
158
+ )
159
+ st.plotly_chart(fig_num, use_container_width=True)
160
+
161
+ st.markdown('<div class="centered-header">Distribución de Monto por Categoría</div>', unsafe_allow_html=True)
162
+
163
+ # graf circular
164
+ fig_pie = px.pie(
165
+ df,
166
+ names="categoria",
167
+ values="mnt_trx_mm",
168
+ hover_data={
169
+ "num_trx": True,
170
+ "pct_mnt_tot": True,
171
+ "pct_num_trx_tot": True
172
+ },
173
+ labels={"mnt_trx_mm": "Monto", "num_trx": "Número de transacciones"},
174
+ hole=0.3
175
+ )
176
+ fig_pie.update_layout(height=500, margin=dict(l=50, r=50, t=20, b=50))
177
+ st.plotly_chart(fig_pie, use_container_width=True)
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
1
+ streamlit==1.32.0
2
+ pandas==2.2.0
3
+ numpy==1.26.4
4
+ requests==2.31.0
5
+ plotly==6.3.0
runtime.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ python_version = "3.11"