File size: 6,499 Bytes
0ef5949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import requests
import plotly.express as px


st.set_page_config(layout="wide")

st.markdown("""

    <style>

    .block-container {

        padding-top: 1rem;

        padding-bottom: 0rem;

        padding-left: 5rem;

        padding-right: 5rem;

        max-width: 100%;

    }

    

    .centered-header {

        text-align: center;

        font-size: 24px;

        font-weight: bold;

        margin-bottom: 10px;

        color: #FFC1C7;

    }

    </style>

    """, unsafe_allow_html=True)


API_URL = "https://9ls1vcbcof.execute-api.us-east-1.amazonaws.com/prueba1-apii/base"

def obtener_datos_cliente(documento):
    doc_api = f"{documento}_"
    params = {"documento": doc_api}
    try:
        response = requests.get(API_URL, params=params)
        response.raise_for_status()
        data = response.json()
        items = data.get("items", [])
        return pd.DataFrame(items)
    except Exception as e:
        st.error(f"Error al consultar la API: {e}")
        return pd.DataFrame()

# ========================
def mostrar_kpi(titulo, valor, color_base="#87CEEB"):
    color_interno = "#D0F0FF"
    color_borde = "#00BFFF"
    st.markdown(
        f"""

        <div style="

            background-color: {color_interno};

            border: 2px solid {color_borde};

            padding: 15px;

            border-radius: 12px;

            text-align: center;

            margin-bottom: 10px;

            box-shadow: 2px 2px 5px rgba(0,0,0,0.15);

            font-size: 18px;

            font-weight: bold;

            color: #000080;

        ">

            {titulo}<br><span style="font-size:22px;">{valor}</span>

        </div>

        """,
        unsafe_allow_html=True
    )

# interfaz streamlit
st.title("Consulta de Datos de Cliente")

doc_input = st.text_input("Ingrese el documento del cliente:", value="-2747430000000000000")

if st.button("Buscar"):
    if doc_input.strip() == "":
        st.warning("Ingrese un documento válido.")
    else:
        df = obtener_datos_cliente(doc_input)
        if df.empty:
            st.info("No se encontraron datos para este documento.")
        else:
            st.subheader(f"Datos del cliente {doc_input}")
            
            # Primera fila: tabla de datos y KPIs a la derecha
            col_tabla, col_kpis = st.columns([0.7, 0.3])
            
            with col_tabla:
                st.dataframe(df)
            
            with col_kpis:
                categoria_mayor_gasto = df.loc[df["mnt_trx_mm"].idxmax(), "categoria"]
                mostrar_kpi("Categoría con mayor gasto", categoria_mayor_gasto)

                categoria_mas_trx = df.loc[df["num_trx"].idxmax(), "categoria"]
                mostrar_kpi("Categoría con más transacciones", categoria_mas_trx)
            
            col_bar1, col_bar2 = st.columns(2)
            
            with col_bar1:
                st.markdown('<div class="centered-header">Monto por Categoría</div>', unsafe_allow_html=True)
                
                # monto por categora
                df_monto = df.sort_values("mnt_trx_mm", ascending=False)
                fig_monto = px.bar(
                    df_monto,
                    x="mnt_trx_mm",
                    y="categoria",
                    orientation="h",
                    text="mnt_trx_mm",
                    hover_data={
                        "categoria": True,
                        "mnt_trx_mm": True,
                        "num_trx": True,
                        "pct_mnt_tot": True,
                        "pct_num_trx_tot": True
                    },
                    labels={"mnt_trx_mm": "Monto", "num_trx": "Número de transacciones"},
                    text_auto=True
                )
                fig_monto.update_traces(textfont_color='darkblue', marker_color="#a1cbe7")
                fig_monto.update_layout(
                    height=500, 
                    margin=dict(l=50, r=50, t=20, b=50),
                    showlegend=False,
                    xaxis_title="Monto",
                    yaxis_title="Categoría"
                )
                st.plotly_chart(fig_monto, use_container_width=True)
            
            with col_bar2:
                st.markdown('<div class="centered-header">Número de Transacciones por Categoría</div>', unsafe_allow_html=True)
                
                # num de transacciones por categoria
                df_num = df.sort_values("num_trx", ascending=False)
                fig_num = px.bar(
                    df_num,
                    x="num_trx",
                    y="categoria",
                    orientation="h",
                    text="num_trx",
                    hover_data={
                        "categoria": True,
                        "mnt_trx_mm": True,
                        "num_trx": True,
                        "pct_mnt_tot": True,
                        "pct_num_trx_tot": True
                    },
                    labels={"num_trx": "Número de transacciones", "mnt_trx_mm": "Monto"},
                    text_auto=True
                )
                fig_num.update_traces(textfont_color='darkgreen', marker_color="#89ffa7")
                fig_num.update_layout(
                    height=500, 
                    margin=dict(l=50, r=50, t=20, b=50),
                    showlegend=False,
                    xaxis_title="Número de Transacciones",
                    yaxis_title="Categoría"
                )
                st.plotly_chart(fig_num, use_container_width=True)
            
            st.markdown('<div class="centered-header">Distribución de Monto por Categoría</div>', unsafe_allow_html=True)
            
            # graf circular
            fig_pie = px.pie(
                df,
                names="categoria",
                values="mnt_trx_mm",
                hover_data={
                    "num_trx": True,
                    "pct_mnt_tot": True,
                    "pct_num_trx_tot": True
                },
                labels={"mnt_trx_mm": "Monto", "num_trx": "Número de transacciones"},
                hole=0.3
            )
            fig_pie.update_layout(height=500, margin=dict(l=50, r=50, t=20, b=50))
            st.plotly_chart(fig_pie, use_container_width=True)