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 seaborn as sns
import numpy as np
st.title("CSV Data Viewer")
url = st.text_input("Enter CSV URL:", "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv")
@st.cache_data
def load_data(url):
data = pd.read_csv(url)
return data
data = load_data(url)
num_rows = st.slider("Select number of rows to display:", 1, min(len(data), 100))
random_shuffle = st.checkbox("Randomly Shuffle DataFrame")
if random_shuffle:
data = data.sample(frac=1).reset_index(drop=True)
st.subheader("DataFrame")
st.dataframe(data.head(num_rows))
st.subheader("Select Variables for Plot")
v_variables = st.multiselect("Choose one or two variables:", options=data.columns)
if len(v_variables) > 0:
if len(v_variables) == 1:
plt.figure(figsize=(10, 5))
sns.histplot(data[v_variables[0]], kde=True)
plt.title(f"Histogram of {v_variables[0]}")
st.pyplot()
elif len(v_variables) == 2:
plt.figure(figsize=(10, 5))
sns.scatterplot(data=data, x=v_variables[0], y=v_variables[1])
plt.title(f"Scatter Plot of {v_variables[0]} vs {v_variables[1]}")
st.pyplot()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?