Spaces:
Sleeping
Sleeping
Update app.py
Browse files17 unmodified lines181920212223242526272829303132333411 unmodified lines46474849505152535455565758596061626364656667686970717273747576777879808182839 unmodified lines939495969798991001011021031041051061071081091101111121131141151168 unmodified lines1251261271281291301311321331341352 unmodified lines13813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017116 unmodified lines18818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423517 unmodified lines# Utilidades# -----------------------------def normalize(s: str) -> str:"""Normaliza un nombre de columna: minúsculas, sin acentos, sin dobles espacios."""s = unicodedata.normalize("NFKD", s).encode("ascii", "ignore").decode("utf-8", "ignore")return " ".join(s.lower().split())def find_target_column(df: pd.DataFrame, target="extskhis_emp full name") -> str | None:"""Encuentra la columna objetivo, siendo tolerante a acentos/espacios/caso."""norm_map = {col: normalize(col) for col in df.columns}for col, norm in norm_map.items():if norm == normalize(target):return col# fallback: columnas muy parecidascandidates = [c for c, n in norm_map.items() if "full" in n and "name" in n]return candidates[0] if candidates else None11 unmodified linesreturn f"{n:,}".replace(",", " ")# -----------------------------# Estilos (UI) – look limpio# -----------------------------CUSTOM_CSS = """<style>/* Fondo suave y tarjetas con glass effect */.stApp { background: linear-gradient(180deg, #f8fafc 0%, #eef2f7 100%); }.block-container { padding-top: 1.5rem; }/* Tarjetas KPI */.kpi-card {border-radius: 14px;padding: 18px 20px;background: rgba(255, 255, 255, 0.75);backdrop-filter: blur(6px);border: 1px solid rgba(0,0,0,0.06);box-shadow: 0 10px 20px -12px rgba(0,0,0,0.12);}.kpi-label { font-size: 0.85rem; color: #5b6573; margin-bottom: 6px; }.kpi-value { font-size: 1.6rem; font-weight: 700; color: #111827; }/* Contenedor principal */.section-card {border-radius: 16px;padding: 20px;background: #ffffff;border: 1px solid #e5e7eb;box-shadow: 0 12px 24px -16px rgba(0,0,0,0.18);}/* Título con acento */h1 span.accent {background: linear-gradient(90deg, #2563eb, #06b6d4);-webkit-background-clip: text;-webkit-text-fill-color: transparent;}9 unmodified linesuploaded = st.sidebar.file_uploader("Sube tu archivo CSV", type=["csv"])sample_note = st.sidebar.empty()# Carga de datos: CSV subido o fallback sampledf = Nonesource_label = ""if uploaded is not None:try:# Usa bytes para mantener cache establedata_bytes = uploaded.getvalue()df = load_csv(io.BytesIO(data_bytes))source_label = f"Fuente: Archivo subido — **{uploaded.name}**"except Exception as e:st.sidebar.error(f"Error al leer el CSV: {e}")else:# intenta cargar sampledf_sample = load_sample("data/sample.csv")if df_sample is not None:df = df_samplesource_label = "Fuente: `data/sample.csv` (muestra)"sample_note.info("No subiste archivo. Mostrando un ejemplo desde `data/sample.csv`.")else:sample_note.warning("No subiste archivo y no existe `data/sample.csv`. Sube un CSV para continuar.")8 unmodified linestarget_col = find_target_column(df, "EXTSKHIS_EMP FULL NAME")if target_col is None:st.error("No se encontró la columna **EXTSKHIS_EMP FULL NAME** (o equivalente). Verifica los encabezados.")st.write("Columnas detectadas:", list(df.columns))st.stop()# Limpieza básica del campo (opcional)df[target_col] = df[target_col].astype(str).str.strip()# -----------------------------2 unmodified lineswith st.sidebar:st.divider()st.subheader("Filtros")search = st.text_input("Filtrar por nombre (contiene)", placeholder="Ej: Maria, Juan...")min_count = st.number_input("Mínimo de ocurrencias", min_value=1, value=1, step=1)top_n = st.slider("Mostrar Top N", min_value=5, max_value=100, value=20, step=5)sort_mode = st.radio("Orden", ["Por conteo (desc)", "Alfabético (A→Z)"], index=0)# Aplica filtro de textodf_filtered = dfif search:s = search.lower()df_filtered = df[df[target_col].str.lower().str.contains(s, na=False)]# Agrega conteoscounts = (df_filtered.groupby(target_col, dropna=False).size().reset_index(name="Count"))# Filtra por mínimo de ocurrenciascounts = counts[counts["Count"] >= min_count]# Ordenaif sort_mode == "Por conteo (desc)":counts = counts.sort_values("Count", ascending=False)else:counts = counts.sort_values(target_col, ascending=True)# Top Ncounts_top = counts.head(top_n)# -----------------------------16 unmodified linesf"<div class='kpi-value'>{pretty_number(len(counts_top))}</div>""</div>", unsafe_allow_html=True)# Fuente de datosst.caption(source_label)# -----------------------------# Gráfico# -----------------------------st.markdown("### 🔎 Conteo por **EXTSKHIS_EMP FULL NAME**")if counts_top.empty:st.warning("No hay filas que cumplan los filtros actuales.")else:# Para mejorar legibilidad en barras, hacemos categoría ordenadacategory_order = counts_top.sort_values("Count", ascending=False)[target_col].tolist()fig = px.bar(counts_top,x="Count",y=target_col,orientation="h",text="Count",category_orders={target_col: category_order},height=600 if len(counts_top) <= 25 else 800,)fig.update_traces(textposition="outside", cliponaxis=False)fig.update_layout(xaxis_title="Conteo",yaxis_title="Nombre",margin=dict(l=10, r=10, t=30, b=10),bargap=0.25,)st.plotly_chart(fig, use_container_width=True, theme="streamlit")# -----------------------------# Tabla de detalle# -----------------------------with st.expander("📄 Ver tabla de conteos"):st.dataframe(counts.reset_index(drop=True), use_container_width=True)# -----------------------------# Descargar resultados# -----------------------------csv_bytes = counts.to_csv(index=False).encode("utf-8")st.download_button(17 unmodified lines181920212223242526272829303111 unmodified lines4344454647484950515253545556575859606162636465666768697071727374757677789 unmodified lines8889909192939495969798991001011021031041051061071088 unmodified lines1171181191201211221231241251262 unmodified lines12913013113213313413513613713813914014114214314414514614714814915015115216 unmodified lines16917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121217 unmodified lines# Utilidades# -----------------------------def normalize(s: str) -> str:s = unicodedata.normalize("NFKD", s).encode("ascii", "ignore").decode("utf-8", "ignore")return " ".join(s.lower().split())def find_target_column(df: pd.DataFrame, target="extskhis_emp full name") -> str | None:norm_map = {col: normalize(col) for col in df.columns}for col, norm in norm_map.items():if norm == normalize(target):return colcandidates = [c for c, n in norm_map.items() if "full" in n and "name" in n]return candidates[0] if candidates else None11 unmodified linesreturn f"{n:,}".replace(",", " ")# -----------------------------# Estilos (UI)# -----------------------------CUSTOM_CSS = """<style>.stApp { background-color: #0d1117; color: #e5e7eb; }.block-container { padding-top: 1.5rem; }/* Tarjetas KPI */.kpi-card {border-radius: 14px;padding: 18px 20px;background: #161b22;border: 1px solid #30363d;box-shadow: 0 8px 16px -10px rgba(0,0,0,0.8);}.kpi-label { font-size: 0.85rem; color: #9ca3af; margin-bottom: 6px; }.kpi-value { font-size: 1.6rem; font-weight: 700; color: #f9fafb; }/* Sección principal */.section-card {border-radius: 16px;padding: 20px;background: #161b22;border: 1px solid #30363d;box-shadow: 0 12px 24px -16px rgba(0,0,0,0.9);}/* Título con acento */h1 span.accent {background: linear-gradient(90deg, #60a5fa, #34d399, #fbbf24);-webkit-background-clip: text;-webkit-text-fill-color: transparent;}9 unmodified linesuploaded = st.sidebar.file_uploader("Sube tu archivo CSV", type=["csv"])sample_note = st.sidebar.empty()df = Nonesource_label = ""if uploaded is not None:try:data_bytes = uploaded.getvalue()df = load_csv(io.BytesIO(data_bytes))source_label = f"Fuente: Archivo subido — **{uploaded.name}**"except Exception as e:st.sidebar.error(f"Error al leer el CSV: {e}")else:df_sample = load_sample("data/sample.csv")if df_sample is not None:df = df_samplesource_label = "Fuente: `data/sample.csv` (muestra)"sample_note.info("No subiste archivo. Mostrando ejemplo.")else:sample_note.warning("No subiste archivo y no existe `data/sample.csv`. Sube un CSV para continuar.")8 unmodified linestarget_col = find_target_column(df, "EXTSKHIS_EMP FULL NAME")if target_col is None:st.error("No se encontró la columna requerida.")st.write("Columnas detectadas:", list(df.columns))st.stop()df[target_col] = df[target_col].astype(str).str.strip()# -----------------------------2 unmodified lineswith st.sidebar:st.divider()st.subheader("Filtros")search = st.text_input("Filtrar por nombre", placeholder="Ej: Maria, Juan...")min_count = st.number_input("Mínimo de ocurrencias", min_value=1, value=1, step=1)top_n = st.slider("Mostrar Top N", min_value=5, max_value=100, value=20, step=5)sort_mode = st.radio("Orden", ["Por conteo (desc)", "Alfabético (A→Z)"], index=0)df_filtered = dfif search:s = search.lower()df_filtered = df[df[target_col].str.lower().str.contains(s, na=False)]counts = df_filtered.groupby(target_col, dropna=False).size().reset_index(name="Count")counts = counts[counts["Count"] >= min_count]if sort_mode == "Por conteo (desc)":counts = counts.sort_values("Count", ascending=False)else:counts = counts.sort_values(target_col, ascending=True)counts_top = counts.head(top_n)# -----------------------------16 unmodified linesf"<div class='kpi-value'>{pretty_number(len(counts_top))}</div>""</div>", unsafe_allow_html=True)st.caption(source_label)# -----------------------------# Gráfico (vertical, colorido, fondo negro)# -----------------------------st.markdown("### 🎨 Conteo por **EXTSKHIS_EMP FULL NAME**")if counts_top.empty:st.warning("No hay filas que cumplan los filtros actuales.")else:fig = px.bar(counts_top,x=target_col,y="Count",text="Count",color=target_col, # cada barra con color distintocolor_discrete_sequence=px.colors.qualitative.Bold, # paleta llamativaheight=600,)fig.update_traces(textposition="outside")fig.update_layout(xaxis_title="Nombre",yaxis_title="Conteo",plot_bgcolor="#0d1117",paper_bgcolor="#0d1117",font=dict(color="white"),margin=dict(l=10, r=10, t=30, b=50),)st.plotly_chart(fig, use_container_width=True)# -----------------------------# Tabla# -----------------------------with st.expander("📄 Ver tabla de conteos"):st.dataframe(counts.reset_index(drop=True), use_container_width=True)# -----------------------------# Descargar# -----------------------------csv_bytes = counts.to_csv(index=False).encode("utf-8")st.download_button(
app.py+33-56