Create an 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 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
@st.cache_data
def load_data(url):
return pd.read_csv(url)
st.title("Penguins Data Viewer")
url = st.text_input("Enter CSV URL:", "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv")
num_rows = st.number_input("Number of rows to display", min_value=1, value=5)
shuffle = st.checkbox("Shuffle DataFrame")
if st.button("Load Data"):
df = load_data(url)
if shuffle:
df = df.sample(frac=1).reset_index(drop=True)
st.write(df.head(num_rows))
variable1 = st.selectbox("Select the first variable:", df.columns)
variable2 = st.selectbox("Select the second variable (optional):", [None] + list(df.columns))
if variable2:
if df[variable1].dtype == 'object' or df[variable2].dtype == 'object':
st.bar_chart(df[[variable1, variable2]].value_counts())
else:
st.line_chart(df[[variable1, variable2]])
else:
if df[variable1].dtype == 'object':
st.bar_chart(df[variable1].value_counts())
else:
st.line_chart(df[variable1])
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?