Create a Streamlit 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 The app should also allow plotting the entire CSV. 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
import matplotlib.pyplot as plt
st.title('Penguin Data Explorer')
# URL input
url = st.text_input('Enter the URL of the CSV file:',
value='https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')
@st.cache_data
def load_data(url):
return pd.read_csv(url)
# Load the data
try:
data = load_data(url)
st.write(data)
except Exception as e:
st.error(f'Error loading data: {e}')
data = pd.DataFrame() # Fallback to empty DataFrame
# Control for number of rows displayed
num_rows = st.slider('Select the number of rows to display:', 1, data.shape[0], 5)
st.dataframe(data.head(num_rows))
# Checkbox for shuffling data
shuffle = st.checkbox('Shuffle DataFrame')
if shuffle:
data = data.sample(frac=1).reset_index(drop=True)
st.write(data.head(num_rows))
# Plotting
if not data.empty:
st.subheader('Plotting Options')
variables = data.select_dtypes(include=['float64', 'int64']).columns.tolist()
x_var = st.selectbox('Select X variable:', variables)
y_var = st.selectbox('Select Y variable (optional):', ['None'] + variables)
if y_var == 'None':
# If only one variable is selected, use a distribution plot
st.write('Displaying distribution of', x_var)
sns.histplot(data[x_var], kde=True)
else:
# Scatter plot for two variables
st.write('Displaying scatter plot for', x_var, 'and', y_var)
sns.scatterplot(data=data, x=x_var, y=y_var)
plt.title(f'{x_var} vs {y_var}' if y_var != 'None' else f'Distribution of {x_var}')
st.pyplot()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?