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
import matplotlib.pyplot as plt
@st.cache_data
def load_data(url):
return pd.read_csv(url)
st.title('Penguins Data Visualization')
url = st.text_input('Enter URL:', 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv')
try:
df = load_data(url)
st.dataframe(df)
rows_to_display = st.slider('Select number of rows to display:', 1, len(df), 10)
shuffled = st.checkbox('Shuffle DataFrame?')
if shuffled:
df = df.sample(frac=1).reset_index(drop=True)
st.dataframe(df.head(rows_to_display))
selected_columns = st.multiselect('Select variables to visualize:', df.columns)
if len(selected_columns) == 1:
st.bar_chart(df[selected_columns[0]].value_counts())
elif len(selected_columns) == 2:
fig, ax = plt.subplots()
ax = df.plot(kind='scatter', x=selected_columns[0], y=selected_columns[1], ax=ax)
st.pyplot(fig)
else:
st.write('Please select one or two variables to visualize.')
except Exception as e:
st.error(f'Error: {e}')
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?