Create an application that has a text field to enter a URL (default value: https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv). The app should read the CSV file (and cache it) from the URL and display a DataFrame. Add controls to do the following: - Control how many rows are displayed - A checkbox to determine to randomly shuffle the DataFrame Users select one or two variables and you must determine the best way to visualize them.
Drop files here
or click to upload
import streamlit as st
import pandas as pd
import plotly.express as px
# Cache the data loading
@st.cache_data
def load_data(url):
return pd.read_csv(url)
# App title
st.title("Data Visualization App")
# URL input
url = st.text_input(
"Enter CSV URL",
value="https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv"
)
try:
# Load the data
df = load_data(url)
# Controls
col1, col2 = st.columns(2)
with col1:
n_rows = st.number_input("Number of rows to display", min_value=1,
max_value=len(df), value=min(5, len(df)))
with col2:
shuffle = st.checkbox("Randomly shuffle data")
if shuffle:
df = df.sample(frac=1).reset_index(drop=True)
# Display the dataframe
st.subheader("Data Preview")
st.dataframe(df.head(n_rows))
# Variable selection
st.subheader("Visualization")
columns = df.select_dtypes(include=['int64', 'float64', 'object']).columns
col1, col2 = st.columns(2)
with col1:
var1 = st.selectbox("Select first variable", columns)
with col2:
var2 = st.selectbox("Select second variable", columns)
# Determine plot type based on variable types
var1_numeric = pd.api.types.is_numeric_dtype(df[var1])
var2_numeric = pd.api.types.is_numeric_dtype(df[var2])
if var1_numeric and var2_numeric:
# Scatter plot for two numeric variables
fig = px.scatter(df, x=var1, y=var2)
st.plotly_chart(fig)
elif var1_numeric and not var2_numeric:
# Box plot for numeric vs categorical
fig = px.box(df, x=var2, y=var1)
st.plotly_chart(fig)
elif not var1_numeric and var2_numeric:
# Box plot for categorical vs numeric
fig = px.box(df, x=var1, y=var2)
st.plotly_chart(fig)
else:
# Bar plot for two categorical variables
counts = df.groupby([var1, var2]).size().reset_index(name='count')
fig = px.bar(counts, x=var1, y='count', color=var2, barmode='group')
st.plotly_chart(fig)
except Exception as e:
st.error(f"Error: {str(e)}")
st.write("Please check if the URL is valid and points to a CSV file.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?