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 seaborn as sns
import numpy as np
# URL for CSV
url = st.text_input('Enter CSV URL', value='https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')
# Load the CSV file with caching
def load_data(url):
data = pd.read_csv(url)
return data
# Read data and display DataFrame
@st.cache_data
def get_data(url):
return load_data(url)
data = get_data(url)
st.write('DataFrame:', data)
# Control for number of rows displayed
num_rows = st.slider('Select number of rows to display', 1, min(len(data), 100), 10)
shuffled = st.checkbox('Shuffle DataFrame')
if shuffled:
data = data.sample(frac=1).reset_index(drop=True)
st.dataframe(data.head(num_rows))
# Visualizing the data
st.write('\n## Data Visualization')
# Select columns for plotting
variables = data.select_dtypes(include=[np.number]).columns.tolist()
col1 = st.selectbox('Select X variable', variables)
col2 = st.selectbox('Select Y variable (optional)', ['None'] + variables)
if col2 != 'None': # if a second variable is selected
plt.figure(figsize=(10, 6))
sns.scatterplot(data=data, x=col1, y=col2)
plt.title(f'Scatter plot of {col1} vs {col2}')
st.pyplot()
else:
plt.figure(figsize=(10, 6))
sns.histplot(data=data, x=col1, bins=30, kde=True)
plt.title(f'Histogram of {col1}')
st.pyplot()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?