Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +78 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,80 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import seaborn as sns
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
st.title("Penguin Dataset Explorer with Seaborn")
|
| 7 |
+
|
| 8 |
+
# Load the dataset
|
| 9 |
+
|
| 10 |
+
df = sns.load_dataset("penguins")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Sidebar filters
|
| 14 |
+
st.sidebar.header("Filters")
|
| 15 |
+
|
| 16 |
+
species_filter = st.sidebar.multiselect(
|
| 17 |
+
"Select Species",
|
| 18 |
+
options=df["species"].unique(),
|
| 19 |
+
default=df["species"].unique()
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
sex_filter = st.sidebar.multiselect(
|
| 23 |
+
"Select Sex",
|
| 24 |
+
options=df["sex"].unique(),
|
| 25 |
+
default=df["sex"].unique()
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Data Filtering
|
| 29 |
+
df_filtered = df[df["species"].isin(species_filter)]
|
| 30 |
+
df_filtered = df_filtered[df_filtered["sex"].isin(sex_filter)]
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Display data
|
| 34 |
+
st.subheader("Filtered Penguin Data")
|
| 35 |
+
st.dataframe(df_filtered.sample(10))
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# Plotting options
|
| 39 |
+
st.subheader("Visualizations")
|
| 40 |
+
|
| 41 |
+
if not df_filtered.empty:
|
| 42 |
+
st.write("---")
|
| 43 |
+
|
| 44 |
+
# Histogram of bill length
|
| 45 |
+
st.subheader("Bill Length Distribution")
|
| 46 |
+
fig_bill_length = plt.figure()
|
| 47 |
+
sns.histplot(df_filtered["bill_length_mm"], kde=True)
|
| 48 |
+
plt.xlabel("Bill Length (mm)")
|
| 49 |
+
plt.ylabel("Frequency")
|
| 50 |
+
plt.title("Bill Length Distribution of Penguins")
|
| 51 |
+
st.pyplot(fig_bill_length)
|
| 52 |
+
|
| 53 |
+
st.write("---")
|
| 54 |
+
|
| 55 |
+
# Scatter plot of bill length vs. flipper length
|
| 56 |
+
st.subheader("Bill Length vs. Flipper Length")
|
| 57 |
+
fig_bill_flipper = plt.figure()
|
| 58 |
+
sns.scatterplot(x="bill_length_mm", y="flipper_length_mm", data=df_filtered)
|
| 59 |
+
plt.xlabel("Bill Length (mm)")
|
| 60 |
+
plt.ylabel("Flipper Length (mm)")
|
| 61 |
+
plt.title("Bill Length vs Flipper Length")
|
| 62 |
+
st.pyplot(fig_bill_flipper)
|
| 63 |
+
|
| 64 |
+
st.write("---")
|
| 65 |
+
|
| 66 |
+
# Boxplot of bill length by species
|
| 67 |
+
st.subheader("Bill Length by Species")
|
| 68 |
+
fig_bill_species = plt.figure()
|
| 69 |
+
sns.boxplot(x="species", y="bill_length_mm", data=df_filtered)
|
| 70 |
+
plt.xlabel("Species")
|
| 71 |
+
plt.ylabel("Bill Length (mm)")
|
| 72 |
+
plt.title("Bill Length by Species")
|
| 73 |
+
st.pyplot(fig_bill_species)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
else:
|
| 77 |
+
st.warning("No data to display after applying filters. Adjust filters or the dataset.")
|
| 78 |
+
|
| 79 |
|
| 80 |
+
st.sidebar.markdown("Data Source: [Seaborn Penguin Dataset](https://seaborn.pydata.org/tutorial/penguins.html)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|