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
# Function to read the CSV and cache the data
@st.cache_data
def load_data(url):
return pd.read_csv(url)
st.title("CSV Data Visualizer")
# Text input for the URL
url = st.text_input("Enter CSV URL:", "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv")
# Load the data
try:
data = load_data(url)
st.write("Data Loaded Successfully.")
st.dataframe(data)
except Exception as e:
st.error(f"Error loading data: {e}")
# Control for number of rows to display
num_rows = st.slider("Select number of rows to display:", min_value=1, max_value=len(data), value=10)
shuffe_data = st.checkbox("Shuffle Data")
# Shuffle the data if the checkbox is checked
if shuffe_data:
data = data.sample(frac=1).reset_index(drop=True)
# Display the requested number of rows
st.dataframe(data.head(num_rows))
# Visualization controls
st.header("Data Visualization")
variables = data.columns.tolist()
# Select one or two variables for visualization
x_var = st.selectbox("Select X Variable:", variables)
num_y = st.radio("Select number of Y Variables:", [1, 2])
if num_y == 1:
y_var = st.selectbox("Select Y Variable:", variables)
# Create a scatter plot or line chart
fig, ax = plt.subplots()
ax.scatter(data[x_var], data[y_var])
ax.set_xlabel(x_var)
ax.set_ylabel(y_var)
ax.set_title(f'Scatter Plot: {x_var} vs {y_var}')
st.pyplot(fig)
else:
y_var1 = st.selectbox("Select First Y Variable:", variables)
y_var2 = st.selectbox("Select Second Y Variable:", variables)
# Create a scatter plot for two variables
fig, ax = plt.subplots()
ax.scatter(data[y_var1], data[y_var2])
ax.set_xlabel(y_var1)
ax.set_ylabel(y_var2)
ax.set_title(f'Scatter Plot: {y_var1} vs {y_var2}')
st.pyplot(fig)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?