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
# Set default URL
DEFAULT_URL = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv'
# Text input for URL
url = st.text_input('Enter URL of the CSV', value=DEFAULT_URL)
@st.cache_data
def load_data(url):
return pd.read_csv(url)
# Load and display the DataFrame
try:
df = load_data(url)
st.write(df)
except Exception as e:
st.error(f'Error loading data: {e}')
# Control for number of rows displayed
num_rows = st.number_input('Select number of rows to display', min_value=1, max_value=df.shape[0], value=10)
shuffled = st.checkbox('Shuffle DataFrame')
if shuffled:
df = df.sample(frac=1).reset_index(drop=True)
st.dataframe(df.head(num_rows))
# Allow users to plot variables
st.subheader('Plotting options')
columns = df.columns.tolist()
var_x = st.selectbox('Select X variable', columns)
var_y = st.selectbox('Select Y variable (optional)', columns + [None])
if var_y:
if st.button('Generate plot'):
fig = px.scatter(df, x=var_x, y=var_y, title=f'Scatter plot of {var_x} vs {var_y}')
st.plotly_chart(fig)
else:
if st.button('Generate plot for single variable'):
fig = px.histogram(df, x=var_x, title=f'Histogram of {var_x}')
st.plotly_chart(fig)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?