Skip to content

1. Resources

1.1 discount_calculator

1.1.1 discount_calculator()

A Streamlit-based interactive discount calculator.

Source code in src/resources/discount_calculator.py
 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
def discount_calculator():
	"""
	A Streamlit-based interactive discount calculator.
	"""

	# Input fields with improved layout
	with st.container():
		original_price = st.number_input(
			"Price before discount (€)", min_value=0.0, value=0.0, format="%.2f"
		)
		discount = st.slider(
			"Discount (%)", min_value=0, max_value=100, value=0, step=1
		)

		saving = original_price * (discount / 100)
		price_after_discount = original_price - saving

		# Display results in columns for better layout
		col1, col2 = st.columns(2)
		with col1:
			st.metric(
				label="Final Price", value=f"€{price_after_discount:.2f}", border=True
			)
		with col2:
			st.metric(label="You Save", value=f"€{saving:.2f}", border=True)

1.2 graphs

1.2.1 display_all_graphs(credentials_status)

Displays various graphs based on the availability of credentials.

Parameters:

Name Type Description Default
credentials_status bool

The status indicating if credentials are valid.

required
Source code in src/resources/graphs.py
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
def display_all_graphs(credentials_status: bool) -> None:
	"""
	Displays various graphs based on the availability of credentials.

	Args:
		credentials_status: The status indicating if credentials are valid.
	"""

	if credentials_status:
		# Show a select box with the years available in the dataset
		available_years = st.session_state.dataframe["Fecha de venta"].dt.year.unique()
		selected_year: str = st.selectbox("Select a year", available_years)

		# Number of products sold month/year
		product_month(df=st.session_state.dataframe, year=int(selected_year))

		# Money earned per month/year
		money_month(df=st.session_state.dataframe, year=int(selected_year))

		# Number of people interested by category in a year
		interest_category(df=st.session_state.dataframe, year=int(selected_year))

		col1, col2 = st.columns(2)
		with col1:
			# Matrix confusion relation between product status and gender
			gender_status(df=st.session_state.dataframe, year=int(selected_year))
		with col2:
			# Matrix confusion relation between product state and country
			status_country(df=st.session_state.dataframe, year=int(selected_year))

1.2.2 gender_status(df, year)

Calculate and visualize the gender distribution by product status for a given year.

Parameters:

Name Type Description Default
df DataFrame

The sales data.

required
year int

The year to filter the data for.

required
Source code in src/resources/graphs.py
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
def gender_status(df: pd.DataFrame, year: int) -> None:
	"""
	Calculate and visualize the gender distribution by product status for a given year.

	Args:
		df: The sales data.
		year: The year to filter the data for.
	"""

	st.subheader("Gender Status Heatmap")

	# Filter data by the selected year
	data_year = df[df["Fecha de venta"].dt.year == year].copy()

	# Group by gender and product status
	df_2dhist = data_year.pivot_table(
		index="Estado del producto",
		columns="Genero",
		values="Fecha de venta",
		aggfunc="count",
		fill_value=0,
	)

	# Create the heatmap using Plotly
	fig = px.imshow(
		df_2dhist,
		text_auto=True,
		color_continuous_scale="mint",
		labels={"color": "Count"},
		aspect="auto",
	)

	# Adjust the layout to remove the grid, axes background transparency, and colorbar
	fig.update_layout(
		xaxis_title="Gender", yaxis_title="Product Status", coloraxis_showscale=False
	)

	# Display the plot in Streamlit
	st.plotly_chart(fig, use_container_width=True)

1.2.3 interest_category(df, year)

Analyzes and visualizes the number of likes per product category for a given year.

Parameters:

Name Type Description Default
df DataFrame

The DataFrame containing sales data.

required
year int

The year for which to filter and analyze the data.

required
Displays

A bar chart in Streamlit showing the number of likes per product category.

Source code in src/resources/graphs.py
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
def interest_category(df: pd.DataFrame, year: int) -> None:
	"""
	Analyzes and visualizes the number of likes per product category for a given year.

	Args:
		df: The DataFrame containing sales data.
		year: The year for which to filter and analyze the data.

	Displays:
		A bar chart in Streamlit showing the number of likes per product category.
	"""

	# Filter data for the selected year
	df_filtered = df[df["Fecha de venta"].dt.year == year].copy()

	# Group by 'Tipo producto' and count 'Numero de mg'
	df_filtered = df_filtered.groupby("Tipo producto")["Numero de mg"].count()

	# Convert the result to a DataFrame
	df_filtered = pd.DataFrame(
		{"Tipo producto": df_filtered.index, "Numero de mg": df_filtered.values}
	)

	# Show graph in Streamlit
	st.subheader(f"Likes by Category in {year}")
	st.bar_chart(
		df_filtered.set_index("Tipo producto"), x_label="Product Type", y_label="Likes"
	)

1.2.4 money_month(df, year)

Calculates and visualizes the total money earned per month for a given year.

Parameters:

Name Type Description Default
df DataFrame

The DataFrame containing sales data.

required
year int

The year for which to calculate the total money earned per month.

required
Source code in src/resources/graphs.py
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
def money_month(df: pd.DataFrame, year: int) -> None:
	"""
	Calculates and visualizes the total money earned per month for a given year.

	Args:
		df: The DataFrame containing sales data.
		year: The year for which to calculate the total money earned per month.
	"""

	# First we filter by year
	df_filtered = df[df["Fecha de venta"].dt.year == year].copy()

	# Then we create a column by month
	df_filtered["Mes"] = df_filtered["Fecha de venta"].dt.month

	# We group the money obtained per month
	df_money_month = df_filtered.groupby("Mes")["Precio producto"].sum()

	# We fill in those months without data with values at 0
	all_months = range(1, 13)
	df_money_month = df_money_month.reindex(all_months, fill_value=0)

	# Convert to DataFrame and include month names
	df_money_month = pd.DataFrame(
		{"Mes": months_names, "Dinero": df_money_month.values}
	)

	# Ensure correct order with pd.Categorical
	df_money_month["Mes"] = pd.Categorical(
		df_money_month["Mes"], categories=months_names, ordered=True
	)

	# Show graph in Streamlit
	st.subheader(f"Money earned per month in {year}")
	st.bar_chart(df_money_month.set_index("Mes"), x_label="Month", y_label="Cash")

1.2.5 product_month(df, year)

summary

Parameters:

Name Type Description Default
df DataFrame

description

required
year int

description

required
Source code in src/resources/graphs.py
 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
def product_month(df: pd.DataFrame, year: int) -> None:
	"""_summary_

	Args:
		df (pd.DataFrame): _description_
		year (int): _description_
	"""

	st.subheader(f"Products sold per month in {year}")

	col1, col2 = st.columns(2, gap="large")

	# Filter data by year
	df_filtered_year = df[df["Fecha de venta"].dt.year == year].copy()

	df_filtered = df_filtered_year.copy()
	df_filtered["Mes"] = df_filtered_year["Fecha de venta"].dt.month
	df_filtered = (
		df_filtered.groupby(["Mes", "Tipo producto"]).size().reset_index(name="count")
	)

	# Convert month numbers to month names
	df_filtered["Mes"] = df_filtered["Mes"].apply(lambda x: months_names[x - 1])

	# Ensure correct order with pd.Categorical
	df_filtered["Mes"] = pd.Categorical(
		df_filtered["Mes"], categories=months_names, ordered=True
	)

	# Pivot for Streamlit bar chart
	df_pivotado = df_filtered.pivot(
		index="Mes", columns="Tipo producto", values="count"
	).fillna(0)

	# Remove months with no products
	df_pivotado = df_pivotado.loc[(df_pivotado > 0).any(axis=1)]

	with col1:
		# Display chart in Streamlit
		st.bar_chart(df_pivotado, horizontal=True, x_label="Products", y_label="Month")
	with col2:
		top_3_products = obtain_top(df=df_filtered_year, top=3, column="Tipo producto")
		st.metric(label="Top 3 products", value=str(top_3_products), border=True)

		a, b = st.columns(2)
		a.metric(
			label="Total products sold",
			value=int(df_filtered_year["Tipo producto"].value_counts().sum()),
			border=True,
		)
		b.metric(
			label="Total cash obtained",
			value=str(float(df_filtered_year["Precio producto"].sum().round(4))) + " €",
			border=True,
		)

1.2.6 status_country(df, year)

Calculate and visualize the product status distribution by country for a given year.

Parameters:

Name Type Description Default
df DataFrame

The sales data.

required
year int

The year to filter the data for.

required

Returns:

Type Description
None

plotly.graph_objs.Figure: The heatmap figure.

Source code in src/resources/graphs.py
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
def status_country(df: pd.DataFrame, year: int) -> None:
	"""
	Calculate and visualize the product status distribution by country for a given year.

	Args:
		df: The sales data.
		year: The year to filter the data for.

	Returns:
		plotly.graph_objs.Figure: The heatmap figure.
	"""

	st.subheader("Product Status by Country Heatmap")

	# Filter data for the specified year
	data_year = df[df["Fecha de venta"].dt.year == year].copy()

	# Group by product status and count the occurrences by country
	df_2dhist = data_year.pivot_table(
		index="Pais",
		columns="Estado del producto",
		values="Fecha de venta",
		aggfunc="count",
		fill_value=0,
	)

	# Create the heatmap using Plotly
	fig = px.imshow(
		df_2dhist,
		text_auto=True,
		color_continuous_scale="mint",
		labels={"color": "Count"},
		aspect="auto",
	)

	# Adjust the layout to remove the grid, axes background transparency, and colorbar
	fig.update_layout(
		xaxis_title="Product Status",
		yaxis_title="Country",
		coloraxis_showscale=False,  # Hide the colorbar
	)

	# Display the plot in Streamlit
	st.plotly_chart(fig, use_container_width=True)

1.3 interact