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
# Function to load data from the URL
@st.cache_data
def load_data(url):
return pd.read_csv(url)
# URL input
url = st.text_input('Enter the URL of the CSV file:',
'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')
# Load and display the data
if url:
df = load_data(url)
st.write("Data from the provided URL:")
st.dataframe(df)
# Control for number of rows displayed
num_rows = st.number_input('Number of rows to display:', min_value=1, max_value=len(df), value=10)
st.dataframe(df.head(num_rows))
# Checkbox to shuffle the DataFrame
shuffle = st.checkbox('Shuffle DataFrame')
if shuffle:
df = df.sample(frac=1).reset_index(drop=True)
st.write('DataFrame has been shuffled.')
st.dataframe(df.head(num_rows))
# Variable selection for plotting
st.write('Select variables for plotting:')
variables = df.columns.tolist()
var1 = st.selectbox('Select the first variable:', variables)
var2 = st.selectbox('Select the second variable (optional):', [None] + variables)
# Create plots based on selections
if var2 is None:
# Univariate plot
fig, ax = plt.subplots()
sns.histplot(df[var1], ax=ax, kde=True)
ax.set_title(f'Histogram of {var1}')
st.pyplot(fig)
else:
# Bivariate plot
fig, ax = plt.subplots()
sns.scatterplot(data=df, x=var1, y=var2, ax=ax)
ax.set_title(f'Scatter plot of {var1} vs {var2}')
st.pyplot(fig)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?