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
# URL input
url = st.text_input('Enter CSV URL:', 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')
# Read and cache the CSV data
@st.cache_data
def load_data(url):
return pd.read_csv(url)
data = load_data(url)
# Display DataFrame controls
num_rows = st.slider('Select number of rows to display', 1, min(10, len(data)), 5)
shuffle = st.checkbox('Shuffle DataFrame')
# Shuffle the DataFrame if selected
if shuffle:
data = data.sample(frac=1).reset_index(drop=True)
# Display the DataFrame
st.write(data.head(num_rows))
# Plotting options
st.subheader('Plotting Options')
# Select variables for plotting
columns = data.select_dtypes(include=['float64', 'int64', 'object']).columns.tolist()
variable_x = st.selectbox('Select variable for X-axis:', columns)
# Check if the selected variable is categorical or numerical
if data[variable_x].dtype in ['float64', 'int64']:
variable_y = st.selectbox('Select variable for Y-axis:', columns)
else:
variable_y = None
# Create a scatter plot or count plot based on selection
if variable_y:
st.subheader('Scatter Plot')
st.scatter_chart(data[[variable_x, variable_y]])
else:
st.subheader('Count Plot')
sns.countplot(x=variable_x, data=data)
st.pyplot()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?