Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,59 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import seaborn as sns
|
| 3 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
st.
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
st.
|
| 13 |
-
|
| 14 |
-
st.write('---')
|
| 15 |
|
| 16 |
-
# Sidebar
|
| 17 |
with st.sidebar:
|
| 18 |
-
st.subheader(
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
else:
|
| 39 |
-
st.
|
| 40 |
-
|
| 41 |
-
st.write('---')
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
+
import seaborn as sns
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
# -------------------------
|
| 7 |
+
# Streamlit App — Tipping
|
| 8 |
+
# -------------------------
|
| 9 |
|
| 10 |
+
# Load dataset
|
| 11 |
+
tips = sns.load_dataset("tips")
|
| 12 |
+
tips["tip_pct"] = tips["tip"] / tips["total_bill"] * 100
|
| 13 |
|
| 14 |
+
# User question
|
| 15 |
+
st.title("💡 Do people tip more on certain days of the week?")
|
| 16 |
+
st.subheader("Explore average tip percentages by day, time.")
|
| 17 |
|
| 18 |
+
# Short problem statement
|
| 19 |
+
st.write("This app helps answer whether tipping behavior changes depending on the day of the week. "
|
| 20 |
+
"Use the filters to explore differences by day and time(Lunch/Dinner) of day.")
|
|
|
|
| 21 |
|
| 22 |
+
# Sidebar filters
|
| 23 |
with st.sidebar:
|
| 24 |
+
st.subheader("Filters")
|
| 25 |
+
# Day filter
|
| 26 |
+
all_days = sorted(tips["day"].dropna().unique().tolist())
|
| 27 |
+
selected_days = st.multiselect("Days to show", options=all_days, default=all_days)
|
| 28 |
+
|
| 29 |
+
# Time filter
|
| 30 |
+
all_times = tips["time"].dropna().unique().tolist()
|
| 31 |
+
selected_time = st.selectbox("Select time of day", options=["All"] + all_times)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Apply filters
|
| 35 |
+
filtered = tips[tips["day"].isin(selected_days)]
|
| 36 |
+
if selected_time != "All":
|
| 37 |
+
filtered = filtered[filtered["time"] == selected_time]
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# KPI: average tip percentage
|
| 41 |
+
avg_tip = filtered["tip_pct"].mean()
|
| 42 |
+
st.metric("For selected days and time , Average Tip %", f"{avg_tip:.2f}%")
|
| 43 |
+
|
| 44 |
+
# Visualization
|
| 45 |
+
if not filtered.empty:
|
| 46 |
+
plt.figure(figsize=(6,4))
|
| 47 |
+
sns.boxplot(x="day", y="tip_pct", data=filtered, order=all_days)
|
| 48 |
+
plt.title("Tip Percentage by Day")
|
| 49 |
+
st.pyplot(plt.gcf())
|
| 50 |
+
plt.close()
|
| 51 |
+
|
| 52 |
+
# Dynamic insight
|
| 53 |
+
best_day = filtered.groupby("day")["tip_pct"].mean().idxmax()
|
| 54 |
+
best_value = filtered.groupby("day")["tip_pct"].mean().max()
|
| 55 |
+
st.success(f"💡 Insight: On average, for {selected_time}, {best_day} has the highest tip percentage at {best_value:.2f}%")
|
| 56 |
else:
|
| 57 |
+
st.info("No data available for the selected filters.")
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|