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 numpy as np
import altair as alt
@st.cache_data
def load_data(url):
return pd.read_csv(url)
# Default URL
url = st.text_input("Enter CSV URL:","https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv")
# Load data from CSV
try:
df = load_data(url)
except Exception as e:
st.error(f"Error loading data: {e}")
st.stop()
# Display DataFrame
st.write("## DataFrame", df)
# Control how many rows to display
num_rows = st.slider("Number of rows to display:", 1, df.shape[0], value=5)
st.write(df.head(num_rows))
# Checkbox to shuffle DataFrame
shuffle = st.checkbox("Shuffle DataFrame")
if shuffle:
df = df.sample(frac=1).reset_index(drop=True)
# Select variables for visualization
variables = df.columns.tolist()
variable1 = st.selectbox("Select first variable:", variables)
variable2 = st.selectbox("Select second variable:", variables)
# Visualize based on selection
if variable1 and variable2:
chart_data = df[[variable1, variable2]].dropna()
if chart_data.shape[1] == 1:
chart = alt.Chart(chart_data).mark_bar().encode(x=variable1, y='count()').properties(width=600, height=400)
else:
chart = alt.Chart(chart_data).mark_circle(size=60).encode(x=variable1, y=variable2, tooltip=[variable1, variable2]).properties(width=600, height=400)
st.altair_chart(chart)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?