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.
To upload files, please first save the app
import streamlit as st
import pandas as pd
import seaborn as sns
@st.cache_data
def load_data(url):
# NOTE: Using corsproxy.io because we're in a WASM environment. If running locally,
# you can remove the corsproxy.io prefix. Some websites don't work with the proxy,
# in those cases try removing the proxy prefix.
proxy_url = f"https://corsproxy.io/?{url}"
return pd.read_csv(proxy_url)
# URL input
url = st.text_input(
"Enter CSV URL",
value="https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv"
)
try:
# Load 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(10, len(df)))
with col2:
shuffle = st.checkbox("Randomly shuffle data")
if shuffle:
df = df.sample(frac=1).reset_index(drop=True)
# Display dataframe
st.write("### Data Preview")
st.dataframe(df.head(n_rows))
# Variable selection
st.write("### Visualization")
st.write("Select variables to visualize:")
# Get numeric and categorical columns
numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
all_cols = df.columns
col1, col2 = st.columns(2)
with col1:
var1 = st.selectbox("Variable 1", all_cols)
with col2:
var2 = st.selectbox("Variable 2", all_cols, index=1 if len(all_cols) > 1 else 0)
# Determine the best visualization based on variable types
var1_numeric = df[var1].dtype in ['float64', 'int64']
var2_numeric = df[var2].dtype in ['float64', 'int64']
fig = sns.jointplot(data=df, x=var1, y=var2)
st.pyplot(fig)
if var1_numeric and var2_numeric:
st.write(f"Correlation between {var1} and {var2}: {df[var1].corr(df[var2]):.2f}")
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?