Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +77 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
import seaborn as sns
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
# Apply the default theme and activate color codes
|
| 10 |
+
sns.set_theme()
|
| 11 |
+
sns.set(color_codes=True)
|
| 12 |
+
|
| 13 |
+
################### import dataset
|
| 14 |
+
penguins = sns.load_dataset("penguins")
|
| 15 |
+
|
| 16 |
+
########### set the title and subtitle
|
| 17 |
+
st.title("Differences between penguins")
|
| 18 |
+
|
| 19 |
+
st.subheader("My flipper is longer!!!")
|
| 20 |
+
########## we add image
|
| 21 |
+
st.image("https://raw.githubusercontent.com/allisonhorst/palmerpenguins/main/man/figures/lter_penguins.png")
|
| 22 |
+
############## we create filters for our interactive plot
|
| 23 |
+
with st.sidebar:
|
| 24 |
+
st.subheader("Filters")
|
| 25 |
+
|
| 26 |
+
all_species = sorted(penguins["species"].dropna().unique().tolist())
|
| 27 |
+
selected_species = st.multiselect(
|
| 28 |
+
"Species to show",
|
| 29 |
+
options=all_species,
|
| 30 |
+
default=all_species,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
feature_options = {
|
| 34 |
+
"Flipper length (mm)": "flipper_length_mm",
|
| 35 |
+
"Bill length (mm)": "bill_length_mm",
|
| 36 |
+
"Bill depth (mm)": "bill_depth_mm",
|
| 37 |
+
"Body mass (g)": "body_mass_g",
|
| 38 |
+
}
|
| 39 |
+
feature_label = st.selectbox("Feature (x-axis)", list(feature_options.keys()))
|
| 40 |
+
x_col = feature_options[feature_label]
|
| 41 |
+
|
| 42 |
+
# KDE options
|
| 43 |
+
fill = st.checkbox("Shade area", value=True)
|
| 44 |
+
bw_adjust = st.slider("Smoothing (bw_adjust)", 0.2, 2.0, 1.0, 0.1)
|
| 45 |
+
common_norm = st.checkbox("Normalize across species", value=False)
|
| 46 |
+
|
| 47 |
+
if not selected_species:
|
| 48 |
+
st.info("Select at least one species to display the plot.")
|
| 49 |
+
else:
|
| 50 |
+
# Filter the data
|
| 51 |
+
data = penguins[penguins["species"].isin(selected_species)].dropna(subset=[x_col, "bill_length_mm"])
|
| 52 |
+
|
| 53 |
+
# Plot using the selected x-axis
|
| 54 |
+
g = sns.relplot(
|
| 55 |
+
data=data,
|
| 56 |
+
x=x_col, # dynamic x-axis
|
| 57 |
+
y="bill_length_mm", # you could also make y selectable similarly
|
| 58 |
+
kind="scatter",
|
| 59 |
+
hue="species"
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
fig = g.fig if hasattr(g, "fig") else g.figure
|
| 63 |
+
st.pyplot(fig)
|
| 64 |
+
plt.close(fig)
|
| 65 |
+
|
| 66 |
+
######## add button to save image
|
| 67 |
+
from io import BytesIO
|
| 68 |
+
|
| 69 |
+
buf = BytesIO()
|
| 70 |
+
fig.savefig(buf, format="png", dpi=200, bbox_inches="tight")
|
| 71 |
+
buf.seek(0)
|
| 72 |
+
st.download_button(
|
| 73 |
+
"Save image",
|
| 74 |
+
data=buf,
|
| 75 |
+
file_name=f"penguins_{x_col}.png",
|
| 76 |
+
mime="image/png",
|
| 77 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.36.0
|
| 2 |
+
pandas==2.2.2
|
| 3 |
+
altair==5.3.0
|
| 4 |
+
pyarrow==16.1.0
|
| 5 |
+
duckdb==1.0.0
|
| 6 |
+
polars==1.5.0
|
| 7 |
+
numpy==1.26.4
|