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
@st.cache_data
def load_data(url):
return pd.read_csv(url)
st.title('Penguins Data Viewer')
url = st.text_input('Enter CSV URL:',
value='https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')
# Load the data
try:
df = load_data(url)
st.success('Data loaded successfully.')
except Exception as e:
st.error(f'Error loading data: {e}')
if not df.empty:
# Control for number of rows displayed
num_rows = st.slider('Select number of rows to display:', 1, min(len(df), 100), 10)
st.dataframe(df.head(num_rows))
# Checkbox for shuffling the DataFrame
shuffle = st.checkbox('Shuffle DataFrame?')
if shuffle:
df = df.sample(frac=1).reset_index(drop=True)
# Select variables for visualization
variable_options = df.columns.tolist()
selected_vars = st.multiselect('Select one or two variables for visualization:', variable_options)
if len(selected_vars) > 0:
if len(selected_vars) == 1:
st.subheader(f'Visualization of {selected_vars[0]}')
st.bar_chart(df[selected_vars[0]].value_counts())
elif len(selected_vars) == 2:
st.subheader(f'Visualization of {selected_vars[0]} vs {selected_vars[1]}')
st.scatter_chart(df, x=selected_vars[0], y=selected_vars[1])
else:
st.warning('Please select one or two variables only.')
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?