deploy_test / src /streamlit_app.py
saoter's picture
Upload streamlit_app.py
b38417a verified
Raw
History Blame Contribute Delete
2.32 kB
import streamlit as st
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from pathlib import Path
st.title("Penguin Dataset Explorer with Seaborn")
# Load the dataset
# Determine the absolute path to the CSV file
csv_path = Path(__file__).parent / "penguins.csv"
# Read the CSV file
df = pd.read_csv(csv_path)
# Sidebar filters
st.sidebar.header("Filters")
species_filter = st.sidebar.multiselect(
"Select Species",
options=df["species"].unique(),
default=df["species"].unique()
)
sex_filter = st.sidebar.multiselect(
"Select Sex",
options=df["sex"].unique(),
default=df["sex"].unique()
)
# Data Filtering
df_filtered = df[df["species"].isin(species_filter)]
df_filtered = df_filtered[df_filtered["sex"].isin(sex_filter)]
# Display data
st.subheader("Filtered Penguin Data")
st.dataframe(df_filtered.sample(10))
# Plotting options
st.subheader("Visualizations")
if not df_filtered.empty:
st.write("---")
# Histogram of bill length
st.subheader("Bill Length Distribution")
fig_bill_length = plt.figure()
sns.histplot(df_filtered["bill_length_mm"], kde=True)
plt.xlabel("Bill Length (mm)")
plt.ylabel("Frequency")
plt.title("Bill Length Distribution of Penguins")
st.pyplot(fig_bill_length)
st.write("---")
# Scatter plot of bill length vs. flipper length
st.subheader("Bill Length vs. Flipper Length")
fig_bill_flipper = plt.figure()
sns.scatterplot(x="bill_length_mm", y="flipper_length_mm", data=df_filtered)
plt.xlabel("Bill Length (mm)")
plt.ylabel("Flipper Length (mm)")
plt.title("Bill Length vs Flipper Length")
st.pyplot(fig_bill_flipper)
st.write("---")
# Boxplot of bill length by species
st.subheader("Bill Length by Species")
fig_bill_species = plt.figure()
sns.boxplot(x="species", y="bill_length_mm", data=df_filtered)
plt.xlabel("Species")
plt.ylabel("Bill Length (mm)")
plt.title("Bill Length by Species")
st.pyplot(fig_bill_species)
else:
st.warning("No data to display after applying filters. Adjust filters or the dataset.")
st.sidebar.markdown("Data Source: [Seaborn Penguin Dataset](https://seaborn.pydata.org/tutorial/penguins.html)")