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 matplotlib.pyplot as plt
import random
st.title('Penguins Data Viewer')
# Text input for URL
url = st.text_input('Enter CSV URL:', 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')
@st.cache_data
def load_data(url):
data = pd.read_csv(url)
return data
# Load data
try:
df = load_data(url)
st.write(df)
except Exception as e:
st.error(f'Error loading data: {e}')
# Control for number of rows to display
num_rows = st.slider('Select number of rows to display:', min_value=1, max_value=len(df), value=10)
# Show the selected number of rows
st.write(df.head(num_rows))
# Checkbox for shuffling
if st.checkbox('Shuffle DataFrame'):
df = df.sample(frac=1).reset_index(drop=True)
st.write(df.head(num_rows))
# Plotting variables
st.subheader('Plotting Variables')
variables = df.columns.tolist()
selected_vars = st.multiselect('Select one or two variables to plot:', variables)
if len(selected_vars) == 1:
# If one variable, create a histogram
plt.figure(figsize=(8, 4))
plt.hist(df[selected_vars[0]], bins=20, color='skyblue', edgecolor='black')
plt.title(f'Histogram of {selected_vars[0]}')
plt.xlabel(selected_vars[0])
plt.ylabel('Frequency')
st.pyplot(plt)
elif len(selected_vars) == 2:
# If two variables, create a scatter plot
plt.figure(figsize=(8, 4))
plt.scatter(df[selected_vars[0]], df[selected_vars[1]], color='orange')
plt.title(f'Scatter Plot of {selected_vars[0]} vs {selected_vars[1]}')
plt.xlabel(selected_vars[0])
plt.ylabel(selected_vars[1])
st.pyplot(plt)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?