-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheese.py
More file actions
303 lines (266 loc) · 11.6 KB
/
cheese.py
File metadata and controls
303 lines (266 loc) · 11.6 KB
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
import os
import requests
import streamlit as st
import pandas as pd
import numpy as np
import time
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import plotly.express as px
from PIL import Image
from streamlit_lottie import st_lottie
# Para obtener la ruta absoluta de los archivos
base_dir = os.path.dirname(os.path.abspath(__file__))
# Obtener la ruta absoluta del archivo css
css_path = os.path.join(base_dir, "style", "main.css")
#funcion carga CSS
def css_loader():
with open(css_path) as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
# Obtener la ruta absoluta del archivo csv
data_path = os.path.join(base_dir, "data", "cheese_clean.csv")
#funcion carga CSV
@st.cache_data
def csv_loader() :
data = pd.read_csv(data_path, delimiter = ",")
#limpia nulos
data['cheese'] = data['cheese'].fillna("Unknown")
return (data)
#funcion carga lottie
def lottie_loader(url):
r = requests.get(url)
if r.status_code !=200:
return None
return r.json()
#carga css
css_loader()
#carga archivo
cheese_df = csv_loader()
email_address ="erik.eudave@gmail.com"
# Obtener la ruta absoluta del archivo gif
gif_path = os.path.join(base_dir, "images", "QuesoAzul.gif")
# --- Define Lottie animation ---
lottie_url = "https://lottie.host/717dcd6d-d113-4163-bbb8-be4b93dfa959/gvSlVjbx3G.json" # Replace with your Lottie URL or load from a local file
lottie_animation = lottie_loader(lottie_url)
# Título
#st.title("🧀 Cheese: Quesos del Mundo!")
# Sidebar (filtros)
with st.sidebar:
#st.header("Filtros")
# Primero contamos cuántos quesos hay por país
country_counts = cheese_df['country'].value_counts().reset_index()
country_counts.columns = ['country', 'count']
# Obtenemos la lista de países ordenados por cantidad de quesos (descendente)
countries_by_cheese_count = country_counts['country'].tolist()
selected_country = st.selectbox(
"País", ["Todos"] + countries_by_cheese_count #sorted(cheese_df['country'].unique().tolist())
)
# Obtenemos la lista de tipos de leche únicos y los ordenamos
selected_milk = st.selectbox(
"Tipo de leche", ["Todos"] + sorted(cheese_df['milk'].dropna().unique().tolist())
)
with st.expander("🧠 Análisis Exploratorio de Datos (EDA)", expanded=False):
st.write(
"El análisis muestra que la mayoría de los quesos provienen de países europeos como Francia e Italia, "
"y se elaboran principalmente con leche de vaca, seguida por cabra y oveja. A través de visualizaciones jerárquicas, "
"se observa una rica diversidad de colores y familias queseras. El análisis de sabor y textura revela grupos diferenciados, "
"y la nube de palabras destaca aromas comunes como cremoso, afrutado e intenso. En conjunto, los datos reflejan la gran variedad "
"sensorial y geográfica del mundo del queso."
)
# Aplicar filtros
filtered_df = cheese_df.copy()
if selected_country != "Todos":
filtered_df = filtered_df[filtered_df['country'] == selected_country]
if selected_milk != "Todos":
filtered_df = filtered_df[filtered_df['milk'] == selected_milk]
#intro
with st.container():
left_column, right_column = st.columns([0.2, 0.8], gap="small", vertical_alignment="center", border=False)
with left_column:
st.markdown('<div class="gif-container">', unsafe_allow_html=True)
st.image(gif_path, width=150)
st.markdown('</div>', unsafe_allow_html=True)
with right_column:
st.title("Cheese: Quesos del Mundo!")
# Mapa
with st.container():
# Crear UNA columna (el (1) hace que devuelva una lista con 1 columna)
columns = st.columns(1) # Devuelve: [col1]
# Contar la cantidad de quesos por país
cheese_counts = filtered_df['country'].value_counts().reset_index()
cheese_counts.columns = ['country', 'cheese_count']
# Acceder a la primera (y única) columna de la lista
with columns[0]: # 👈 Usar el primer elemento de la lista
with st.container(border=True, key="figura0"):
st.subheader("🌍 Mapa de Quesos por País")
# Crear el mapa
fig = px.choropleth(cheese_counts,
locations='country',
locationmode='country names',
color='cheese_count',
hover_name='country',
color_continuous_scale=[
[0, '#fab75a'], # Naranja claro
[0.5, '#cc7d0e'], # Naranja oscuro
[1, '#8f5401'] # Naranja mas oscuro
])
# Actualizar el layout para cambiar el color de fondo
fig.update_layout(
margin={"r": 0, "t": 0, "l": 0, "b": 0},
paper_bgcolor='#082e6e', # Color de fondo alrededor del mapa
geo=dict(
bgcolor='#082e6e', # Color de fondo del mapa
lakecolor='#ffffff', # Color de los lagos
landcolor='#ffffff', # Color de la tierra (áreas sin datos)
showland=True,
showlakes=True,
showocean=True,
oceancolor='#d6e9ff', # Color del océano
projection_type='natural earth'
)
)
# Mostrar el mapa en Streamlit ajustado al contenedor
st.plotly_chart(fig, use_container_width=True)
# Visualizaciones 1
with st.container():
#st.write("---")
columns = st.columns(1)
with columns[0]:
with st.container(border=True, key="figura1"):
st.subheader("🧀 Quesos por País y Tipo de Leche")
# Paso 1: Calcular el total de quesos por país para determinar el orden
total_by_country = filtered_df.groupby('country').size().reset_index(name='total_count')
total_by_country = total_by_country.sort_values('total_count', ascending=False)
# Paso 2: Obtener la lista ordenada de países
ordered_countries = total_by_country['country'].tolist()
# Paso 3: Agrupar por país y tipo de leche
milk_country_counts = filtered_df.groupby(['country', 'milk']).size().reset_index(name='count')
# Paso 4: Crear una columna categórica ordenada para los países
milk_country_counts['country_ordered'] = pd.Categorical(
milk_country_counts['country'],
categories=ordered_countries,
ordered=True
)
# Paso 5: Ordenar primero por la columna categórica (país) y luego por conteo dentro de cada país
milk_country_counts = milk_country_counts.sort_values(
['country_ordered', 'count'],
ascending=[True, False])
# Crear la gráfica de barras con tipo de leche como hue (color)
fig = px.bar(
milk_country_counts,
x='country',
y='count',
color='milk', # Aquí agregamos el tipo de leche como hue
orientation='v',
color_discrete_sequence=px.colors.sequential.Oranges_r,
barmode='stack', # Puedes usar 'stack' para barras apiladas o 'group' para barras agrupadas
category_orders={"country": ordered_countries}, # Esto garantiza el orden correcto en el eje x
height=500
)
fig.update_layout(
xaxis_title="País",
yaxis_title="Número de Quesos",
legend_title="Tipo de Leche",
paper_bgcolor="#082e6e", # Fondo de la figura
plot_bgcolor="#082e6e", # Fondo del área del gráfico
font=dict(color="#fc8c1c"), # Color del texto
legend=dict(
font=dict(color="#f5eec6"),
title_font=dict(color="#fc8c1c")
),
margin=dict(l=0, r=5, t=0, b=0) # Reduce márgenes
)
fig.update_xaxes(tickangle=45)
# Mostrar la gráfica en Streamlit
st.plotly_chart(fig, use_container_width=True)
# Visualizaciones 2
with st.container():
#st.write("---")
columns = st.columns(1)
with columns[0]:
with st.container(border=True, key="figura2"):
st.subheader("🎨 Quesos por Color y Familia")
fig = px.sunburst(
filtered_df,
path=['family', 'color'],
color_discrete_sequence=px.colors.sequential.YlOrBr_r,
height=500
)
fig.update_layout(
paper_bgcolor="#082e6e",
plot_bgcolor="#082e6e",
font=dict(color="#fc8c1c"),
margin=dict(l=0, r=0, t=0, b=0) # Reduce márgenes
)
st.plotly_chart(fig, use_container_width=True)
# Visualizaciones 3
with st.container():
#st.write("---")
columns = st.columns(1)
with columns[0]:
with st.container(border=True, key="figura3"):
st.subheader("👅 Relación Sabor Vs Textura")
fig = px.scatter(
filtered_df,
x='texture',
y='flavor',
color='milk',
color_discrete_sequence=px.colors.sequential.Oranges_r, # Paleta para categorías discretas
hover_name='cheese',
height=500
)
fig.update_layout(
xaxis_title="Textura",
yaxis_title="Sabor",
legend_title="Tipo de Leche",
paper_bgcolor="#082e6e", # Fondo de la figura
plot_bgcolor="#082e6e", # Fondo del área del gráfico
font=dict(color="#fc8c1c"), # Color del texto
legend=dict(
font=dict(color="#f5eec6"),
title_font=dict(color="#fc8c1c")
),
margin=dict(l=0, r=0, t=0, b=0) # Reduce márgenes
)
fig.update_xaxes(tickangle=45)
st.plotly_chart(fig, use_container_width=True)
# Visualizaciones 4
with st.container():
#st.write("---")
columns = st.columns(1)
with columns[0]:
with st.container(border=True, key="figura4"):
st.subheader("👃 Principales Aromas")
text = " ".join(filtered_df['aroma'].dropna())
wordcloud = WordCloud(
background_color='white',
colormap='Oranges_r',
width=800,
height=400
).generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
st.pyplot(plt)
# Buscador de quesos
with st.container():
#st.write("---")
# Buscador de quesos
st.subheader("Buscador por nombre")
search_term = st.text_input("Escribe el nombre de un queso:")
if search_term:
results = filtered_df[filtered_df['cheese'].str.contains(search_term, case=False)]
st.dataframe(results)
with st.container():
st.write("---")
# Dataframe
with st.expander(f"Ver tabla de Quesos ({len(filtered_df)})", expanded=False):
st.dataframe(
filtered_df,
use_container_width=True
)
# --- PIE DE PÁGINA ---
st.markdown("---")
st.markdown("""
Aplicación desarrollada por [Erik Eudave ⚙️](https://github.com/eeudave/) |
Datos: [Kaggle Dataset](https://www.kaggle.com/datasets/umerhaddii/global-cheese-dataset/)
""")