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.
Drop files here
or click to upload
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Function to load data
@st.cache_data
def load_data(url):
return pd.read_csv(url)
# Default URL
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv"
# Title of the app
st.title("Penguins Data Viewer")
# URL input
url_input = st.text_input("Enter CSV URL:", value=url)
data = load_data(url_input)
# Control for number of rows displayed
num_rows = st.slider("Select number of rows to display:", 1, data.shape[0], 5)
# Show DataFrame
st.write(data.head(num_rows))
# Checkbox to shuffle the DataFrame
shuffle = st.checkbox("Shuffle DataFrame")
if shuffle:
data = data.sample(frac=1).reset_index(drop=True)
st.write(data.head(num_rows))
# Controls for plotting
st.subheader("Plotting Variables")
vars = data.select_dtypes(include=[np.number]).columns.tolist()
selected_vars = st.multiselect("Choose variables to plot:", vars, default=vars[:2])
if len(selected_vars) == 1:
# Scatter plot for single variable
st.write(f"### Plot for {selected_vars[0]}")
plt.figure(figsize=(10, 6))
plt.hist(data[selected_vars[0]], bins=20, color='blue', alpha=0.7)
plt.xlabel(selected_vars[0])
plt.ylabel("Frequency")
st.pyplot()
elif len(selected_vars) == 2:
# Scatter plot for two variables
st.write(f"### Scatter plot of {selected_vars[0]} vs {selected_vars[1]}")
plt.figure(figsize=(10, 6))
plt.scatter(data[selected_vars[0]], data[selected_vars[1]], alpha=0.7)
plt.xlabel(selected_vars[0])
plt.ylabel(selected_vars[1])
st.pyplot()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?