Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| # Apply the default theme and activate color codes | |
| sns.set_theme() | |
| sns.set(color_codes=True) | |
| # Import the dataset | |
| tips = sns.load_dataset("tips") | |
| tips["tip_percentage"] = tips["tip"] / tips["total_bill"] * 100 | |
| # Create the title and subtitle | |
| st.title("How does the amount of tip / percentage of tips differ across different days of the week?") | |
| 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.") | |
| # create filters/sidebars for our interactive plot | |
| with st.sidebar: | |
| st.subheader("Filters") | |
| # Select the day | |
| all_days = sorted(tips["day"].unique()) | |
| selected_days = st.multiselect( | |
| "Days to show", | |
| options=all_days, | |
| default=all_days, | |
| ) | |
| # Select the x-axis | |
| feature_options = { | |
| "Tip": "tip", | |
| "Tip Percentage": "tip_percentage" | |
| } | |
| feature_label = st.selectbox("Feature (x-axis)", list(feature_options.keys())) | |
| x_col = feature_options[feature_label] | |
| # enable fill options | |
| fill = st.checkbox("Shade area", value=True) | |
| if not selected_days: | |
| st.info("Select at least one day to display the plot.") | |
| else: | |
| # Filter the data | |
| data = tips[tips["day"].isin(selected_days)].dropna(subset=[x_col]) | |
| # Make/show the KPI | |
| avg_value = data[x_col].mean() | |
| unit = "$" if x_col == "tip" else "%" | |
| st.metric( | |
| label=f"Average {feature_label} for selected days", | |
| value=f"{avg_value:.2f} {unit}" | |
| ) | |
| # The plot itself | |
| g = sns.displot( | |
| data=data, | |
| x=x_col, | |
| hue="day", | |
| kind="kde", | |
| fill=fill | |
| ) | |
| fig = g.fig | |
| st.pyplot(fig) | |
| plt.close(fig) | |
| # Adding the dynamic text | |
| max_day = ( | |
| data.groupby("day")[x_col].mean() | |
| .sort_values(ascending=False) | |
| .index[0] | |
| ) | |
| st.success( | |
| f"π‘ On average, **{max_day}** has the highest {feature_label.lower()} among the selected days." | |
| ) | |