Spaces:
Sleeping
Sleeping
Upload assignment_app.py
Browse files- assignment_app.py +77 -0
assignment_app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
# Apply the default theme and activate color codes
|
| 8 |
+
sns.set_theme()
|
| 9 |
+
sns.set(color_codes=True)
|
| 10 |
+
|
| 11 |
+
# Import the dataset
|
| 12 |
+
tips = sns.load_dataset("tips")
|
| 13 |
+
tips["tip_percentage"] = tips["tip"] / tips["total_bill"] * 100
|
| 14 |
+
|
| 15 |
+
# Create the title and subtitle
|
| 16 |
+
st.title("How does the amount of tip / percentage of tips differ across different days of the week?")
|
| 17 |
+
st.subheader("This app shows which days of the week bring in higher tip percentages and tip amounts, helping restaurants and staff adapt to customer tipping behavior and optimize their business.")
|
| 18 |
+
|
| 19 |
+
# create filters/sidebars for our interactive plot
|
| 20 |
+
with st.sidebar:
|
| 21 |
+
st.subheader("Filters")
|
| 22 |
+
|
| 23 |
+
# Select the day
|
| 24 |
+
all_days = sorted(tips["day"].unique())
|
| 25 |
+
selected_days = st.multiselect(
|
| 26 |
+
"Days to show",
|
| 27 |
+
options=all_days,
|
| 28 |
+
default=all_days,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Select the x-axis
|
| 32 |
+
feature_options = {
|
| 33 |
+
"Tip": "tip",
|
| 34 |
+
"Tip Percentage": "tip_percentage"
|
| 35 |
+
}
|
| 36 |
+
feature_label = st.selectbox("Feature (x-axis)", list(feature_options.keys()))
|
| 37 |
+
x_col = feature_options[feature_label]
|
| 38 |
+
|
| 39 |
+
# enable fill options
|
| 40 |
+
fill = st.checkbox("Shade area", value=True)
|
| 41 |
+
|
| 42 |
+
if not selected_days:
|
| 43 |
+
st.info("Select at least one day to display the plot.")
|
| 44 |
+
else:
|
| 45 |
+
# Filter the data
|
| 46 |
+
data = tips[tips["day"].isin(selected_days)].dropna(subset=[x_col])
|
| 47 |
+
|
| 48 |
+
# Make/show the KPI
|
| 49 |
+
avg_value = data[x_col].mean()
|
| 50 |
+
unit = "$" if x_col == "tip" else "%"
|
| 51 |
+
st.metric(
|
| 52 |
+
label=f"Average {feature_label} for selected days",
|
| 53 |
+
value=f"{avg_value:.2f} {unit}"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# The plot itself
|
| 57 |
+
g = sns.displot(
|
| 58 |
+
data=data,
|
| 59 |
+
x=x_col,
|
| 60 |
+
hue="day",
|
| 61 |
+
kind="kde",
|
| 62 |
+
fill=fill
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
fig = g.fig
|
| 66 |
+
st.pyplot(fig)
|
| 67 |
+
plt.close(fig)
|
| 68 |
+
|
| 69 |
+
# Adding the dynamic text
|
| 70 |
+
max_day = (
|
| 71 |
+
data.groupby("day")[x_col].mean()
|
| 72 |
+
.sort_values(ascending=False)
|
| 73 |
+
.index[0]
|
| 74 |
+
)
|
| 75 |
+
st.success(
|
| 76 |
+
f"💡 On average, **{max_day}** has the highest {feature_label.lower()} among the selected days."
|
| 77 |
+
)
|