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 altair as alt
# Cache data loading
@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.
if not url.startswith('https://corsproxy.io/?'):
url = f'https://corsproxy.io/?{url}'
return pd.read_csv(url)
# Main app
st.title('Data Visualization')
# 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.slider('Number of rows to display', 1, len(df), 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.subheader('Data Preview')
st.dataframe(df.head(n_rows))
# Variable selection
st.subheader('Visualization')
numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
categorical_cols = df.select_dtypes(include=['object']).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)
if var1 != 'None':
if var2 == 'None':
# Single variable visualization
if var1 in numeric_cols:
# Histogram for numeric variables
chart = alt.Chart(df).mark_bar().encode(
alt.X(var1, bin=True),
y='count()'
).properties(
title=f'Distribution of {var1}'
)
st.altair_chart(chart, use_container_width=True)
else:
# Bar chart for categorical variables
chart = alt.Chart(df).mark_bar().encode(
x='count()',
y=var1
).properties(
title=f'Distribution of {var1}'
)
st.altair_chart(chart, use_container_width=True)
else:
# Two variable visualization
if var1 in numeric_cols and var2 in numeric_cols:
# Scatter plot for two numeric variables
chart = alt.Chart(df).mark_circle().encode(
x=var1,
y=var2,
tooltip=[var1, var2]
).properties(
title=f'{var1} vs {var2}'
)
st.altair_chart(chart, use_container_width=True)
elif var1 in numeric_cols and var2 in categorical_cols:
# Box plot for numeric vs categorical
chart = alt.Chart(df).mark_boxplot().encode(
x=var2,
y=var1
).properties(
title=f'{var1} by {var2}'
)
st.altair_chart(chart, use_container_width=True)
elif var1 in categorical_cols and var2 in numeric_cols:
# Box plot for numeric vs categorical
chart = alt.Chart(df).mark_boxplot().encode(
x=var1,
y=var2
).properties(
title=f'{var2} by {var1}'
)
st.altair_chart(chart, use_container_width=True)
else:
# Heatmap for two categorical variables
chart = alt.Chart(df).mark_rect().encode(
x=var1,
y=var2,
color='count()'
).properties(
title=f'{var1} vs {var2}'
)
st.altair_chart(chart, use_container_width=True)
except Exception as e:
st.error(f"Error: {str(e)}")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?