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 plotly.express as px
import seaborn as sns
@st.cache_data
def load_data(url):
return pd.read_csv(url)
# App title
st.title("CSV Data Visualizer")
# 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, random_state=42).reset_index(drop=True)
# Display dataframe
st.subheader("Data Preview")
st.dataframe(df.head(n_rows))
# Variable selection
st.subheader("Visualization")
# Get numeric and categorical columns
numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
categorical_cols = df.select_dtypes(include=['object', 'category']).columns
all_cols = pd.Index(['None']).append(df.columns)
col1, col2 = st.columns(2)
with col1:
var1 = st.selectbox("Select first variable", all_cols)
with col2:
var2 = st.selectbox("Select second variable", all_cols)
# Visualization logic
if var1 != 'None':
if var2 == 'None':
# Single variable visualization
if var1 in numeric_cols:
fig = px.histogram(df, x=var1, title=f'Distribution of {var1}')
st.plotly_chart(fig)
else:
fig = px.bar(df[var1].value_counts().reset_index(),
x='index', y=var1,
title=f'Count of {var1}')
st.plotly_chart(fig)
else:
# Two variable visualization
if var1 in numeric_cols and var2 in numeric_cols:
# Scatter plot for two numeric variables
fig = px.scatter(df, x=var1, y=var2,
title=f'{var2} vs {var1}')
st.plotly_chart(fig)
elif var1 in numeric_cols and var2 in categorical_cols:
# Box plot for numeric vs categorical
fig = px.box(df, x=var2, y=var1,
title=f'{var1} distribution by {var2}')
st.plotly_chart(fig)
elif var1 in categorical_cols and var2 in numeric_cols:
# Box plot for numeric vs categorical
fig = px.box(df, x=var1, y=var2,
title=f'{var2} distribution by {var1}')
st.plotly_chart(fig)
else:
# Heatmap for two categorical variables
heatmap_data = pd.crosstab(df[var1], df[var2])
fig = px.imshow(heatmap_data,
title=f'Heatmap of {var1} vs {var2}')
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?