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
# Function to load data from URL
@st.cache_data
def load_data(url):
return pd.read_csv(url)
st.title('Penguins Data Visualizer')
# Text input for URL
url = st.text_input('Enter CSV URL:', 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')
# Load data
try:
data = load_data(url)
st.write(data)
# Control to select number of rows displayed
num_rows = st.slider('Select number of rows to display:', 1, len(data), 5)
st.write(data.head(num_rows))
# Checkbox to shuffle the DataFrame
shuffle = st.checkbox('Shuffle data')
if shuffle:
data = data.sample(frac=1).reset_index(drop=True)
# Selecting variables for plotting
numeric_columns = data.select_dtypes(include=['float64', 'int64']).columns.tolist()
x_var = st.selectbox('Select x-axis variable:', numeric_columns)
y_var = st.selectbox('Select y-axis variable (leave as None for univariate):', ['None'] + numeric_columns)
# Plotting the data
if st.button('Plot Data'):
plt.figure(figsize=(10, 5))
if y_var == 'None':
# Univariate plot
data[x_var].plot(kind='hist')
plt.title(f'Histogram of {x_var}')
else:
# Bivariate plot
data.plot.scatter(x=x_var, y=y_var)
plt.title(f'Scatter plot of {y_var} vs {x_var}')
plt.xlabel(x_var)
plt.ylabel(y_var if y_var != 'None' else 'Frequency')
st.pyplot()
except Exception as e:
st.error(f'Error loading data: {e}')
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?