Spaces:
Sleeping
Sleeping
Commit
路
07b9198
1
Parent(s):
5e03bf7
CSV
Browse files- .DS_Store +0 -0
- .gitignore +0 -0
- app.py +152 -0
- requirements.txt +6 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.gitignore
ADDED
|
File without changes
|
app.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
# Configuraci贸n de la p谩gina
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Visualizador de Datos",
|
| 12 |
+
page_icon="馃搳",
|
| 13 |
+
layout="wide"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# T铆tulo de la aplicaci贸n
|
| 17 |
+
st.title("馃搳 Visualizador de Datos")
|
| 18 |
+
st.markdown("### Carga tu archivo CSV o Excel y crea visualizaciones personalizadas")
|
| 19 |
+
|
| 20 |
+
# Funci贸n para cargar el archivo
|
| 21 |
+
def load_data():
|
| 22 |
+
uploaded_file = st.file_uploader("Carga tu archivo CSV o Excel", type=["csv", "xlsx", "xls"])
|
| 23 |
+
|
| 24 |
+
if uploaded_file is not None:
|
| 25 |
+
try:
|
| 26 |
+
# Determinar el tipo de archivo y cargarlo
|
| 27 |
+
if uploaded_file.name.endswith('.csv'):
|
| 28 |
+
data = pd.read_csv(uploaded_file)
|
| 29 |
+
else:
|
| 30 |
+
data = pd.read_excel(uploaded_file)
|
| 31 |
+
|
| 32 |
+
return data
|
| 33 |
+
except Exception as e:
|
| 34 |
+
st.error(f"Error al cargar el archivo: {e}")
|
| 35 |
+
return None
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
# Funci贸n para generar gr谩ficos
|
| 39 |
+
def create_plot(data, x_col, y_col, plot_type):
|
| 40 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 41 |
+
|
| 42 |
+
if plot_type == "Barras":
|
| 43 |
+
sns.barplot(x=x_col, y=y_col, data=data, ax=ax)
|
| 44 |
+
elif plot_type == "L铆neas":
|
| 45 |
+
sns.lineplot(x=x_col, y=y_col, data=data, ax=ax)
|
| 46 |
+
elif plot_type == "Dispersi贸n":
|
| 47 |
+
sns.scatterplot(x=x_col, y=y_col, data=data, ax=ax)
|
| 48 |
+
elif plot_type == "Histograma":
|
| 49 |
+
sns.histplot(data[x_col], ax=ax)
|
| 50 |
+
plt.xlabel(x_col)
|
| 51 |
+
elif plot_type == "Boxplot":
|
| 52 |
+
sns.boxplot(x=x_col, y=y_col, data=data, ax=ax)
|
| 53 |
+
elif plot_type == "Viol铆n":
|
| 54 |
+
sns.violinplot(x=x_col, y=y_col, data=data, ax=ax)
|
| 55 |
+
elif plot_type == "Pastel":
|
| 56 |
+
data[x_col].value_counts().plot.pie(autopct='%1.1f%%', ax=ax)
|
| 57 |
+
plt.ylabel('')
|
| 58 |
+
elif plot_type == "Mapa de calor":
|
| 59 |
+
if len(data) > 100:
|
| 60 |
+
sample_data = data.sample(100)
|
| 61 |
+
else:
|
| 62 |
+
sample_data = data
|
| 63 |
+
correlation = sample_data.select_dtypes(include=['float64', 'int64']).corr()
|
| 64 |
+
sns.heatmap(correlation, annot=True, cmap='coolwarm', ax=ax)
|
| 65 |
+
|
| 66 |
+
plt.tight_layout()
|
| 67 |
+
return fig
|
| 68 |
+
|
| 69 |
+
# Funci贸n para descargar im谩genes
|
| 70 |
+
def get_image_download_link(fig, filename, text):
|
| 71 |
+
buf = io.BytesIO()
|
| 72 |
+
fig.savefig(buf, format='png', dpi=300, bbox_inches='tight')
|
| 73 |
+
buf.seek(0)
|
| 74 |
+
b64 = base64.b64encode(buf.read()).decode()
|
| 75 |
+
href = f'<a href="data:image/png;base64,{b64}" download="{filename}.png">{text}</a>'
|
| 76 |
+
return href
|
| 77 |
+
|
| 78 |
+
# Funci贸n principal
|
| 79 |
+
def main():
|
| 80 |
+
# Cargar datos
|
| 81 |
+
data = load_data()
|
| 82 |
+
|
| 83 |
+
if data is not None:
|
| 84 |
+
# Mostrar informaci贸n b谩sica del dataset
|
| 85 |
+
st.subheader("Vista previa de los datos")
|
| 86 |
+
st.dataframe(data.head())
|
| 87 |
+
|
| 88 |
+
st.subheader("Informaci贸n del dataset")
|
| 89 |
+
col1, col2 = st.columns(2)
|
| 90 |
+
with col1:
|
| 91 |
+
st.info(f"N煤mero de filas: {data.shape[0]}")
|
| 92 |
+
with col2:
|
| 93 |
+
st.info(f"N煤mero de columnas: {data.shape[1]}")
|
| 94 |
+
|
| 95 |
+
# Selecci贸n de columnas y tipo de gr谩fico
|
| 96 |
+
st.subheader("Crear visualizaci贸n")
|
| 97 |
+
|
| 98 |
+
col1, col2, col3 = st.columns(3)
|
| 99 |
+
|
| 100 |
+
with col1:
|
| 101 |
+
plot_type = st.selectbox(
|
| 102 |
+
"Tipo de gr谩fico",
|
| 103 |
+
["Barras", "L铆neas", "Dispersi贸n", "Histograma", "Boxplot", "Viol铆n", "Pastel", "Mapa de calor"]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Opciones de columnas basadas en el tipo de gr谩fico
|
| 107 |
+
numeric_cols = data.select_dtypes(include=['float64', 'int64']).columns.tolist()
|
| 108 |
+
categorical_cols = data.select_dtypes(include=['object']).columns.tolist()
|
| 109 |
+
all_cols = data.columns.tolist()
|
| 110 |
+
|
| 111 |
+
with col2:
|
| 112 |
+
if plot_type == "Histograma":
|
| 113 |
+
x_col = st.selectbox("Selecciona la columna para el histograma", numeric_cols)
|
| 114 |
+
y_col = None
|
| 115 |
+
elif plot_type == "Mapa de calor":
|
| 116 |
+
x_col = "Correlaci贸n"
|
| 117 |
+
y_col = "Correlaci贸n"
|
| 118 |
+
elif plot_type == "Pastel":
|
| 119 |
+
x_col = st.selectbox("Selecciona la columna para el gr谩fico de pastel", categorical_cols if categorical_cols else all_cols)
|
| 120 |
+
y_col = None
|
| 121 |
+
else:
|
| 122 |
+
x_options = categorical_cols + numeric_cols if categorical_cols else all_cols
|
| 123 |
+
x_col = st.selectbox("Selecciona la columna para el eje X", x_options)
|
| 124 |
+
|
| 125 |
+
with col3:
|
| 126 |
+
if plot_type not in ["Histograma", "Pastel", "Mapa de calor"]:
|
| 127 |
+
y_col = st.selectbox("Selecciona la columna para el eje Y", numeric_cols if numeric_cols else all_cols)
|
| 128 |
+
|
| 129 |
+
# Crear gr谩fico
|
| 130 |
+
if st.button("Generar visualizaci贸n"):
|
| 131 |
+
try:
|
| 132 |
+
st.subheader("Visualizaci贸n")
|
| 133 |
+
fig = create_plot(data, x_col, y_col, plot_type)
|
| 134 |
+
st.pyplot(fig)
|
| 135 |
+
|
| 136 |
+
# Bot贸n para descargar la imagen
|
| 137 |
+
st.markdown(
|
| 138 |
+
get_image_download_link(
|
| 139 |
+
fig,
|
| 140 |
+
f"{plot_type}_{x_col}_{y_col if y_col else ''}",
|
| 141 |
+
"馃摜 Descargar imagen"
|
| 142 |
+
),
|
| 143 |
+
unsafe_allow_html=True
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
except Exception as e:
|
| 147 |
+
st.error(f"Error al generar el gr谩fico: {e}")
|
| 148 |
+
st.info("Sugerencia: Verifica que las columnas seleccionadas sean compatibles con el tipo de gr谩fico.")
|
| 149 |
+
|
| 150 |
+
# Ejecutar la aplicaci贸n
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.22.0
|
| 2 |
+
pandas==1.5.3
|
| 3 |
+
matplotlib==3.7.1
|
| 4 |
+
seaborn==0.12.2
|
| 5 |
+
openpyxl==3.1.2
|
| 6 |
+
Pillow==9.5.0
|