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 plotly.express as px
# Function to load data
@st.cache_data
def load_data(url):
return pd.read_csv(url)
# Default URL
url = st.text_input('Enter CSV URL', 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')
# Load and display data
try:
data = load_data(url)
st.write('Data loaded successfully!')
st.dataframe(data)
# Controls for displaying rows and shuffling data
num_rows = st.slider('Select number of rows to display', 1, data.shape[0], 5)
shuffle_data = st.checkbox('Shuffle DataFrame?')
if shuffle_data:
data = data.sample(frac=1).reset_index(drop=True)
st.dataframe(data.head(num_rows))
# Plotting
st.subheader('Plotting Variables')
columns = data.columns.tolist()
x_axis = st.selectbox('Select X-axis variable', columns)
y_axis = st.selectbox('Select Y-axis variable (optional)', ['None'] + columns)
if y_axis != 'None':
fig = px.scatter(data, x=x_axis, y=y_axis)
st.plotly_chart(fig)
else:
fig = px.histogram(data, x=x_axis)
st.plotly_chart(fig)
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?