To upload files, please first save the app
import streamlit as st
import pandas as pd
import seaborn as sns
# Cache data loading (prevent reloading)
@st.cache_data
def load_data(url):
return pd.read_csv(url)
# Title of the app
# Data URL input with default value
url = st.text_input('CSV file URL', value='https://raw.githubusercontent.com/mwaskom/seaborn-data/refs/heads/master/titanic.csv')
try:
# Load the data
df = load_data(url)
# Table Display - changing number of rows
n_rows = st.number_input("Number of rows to display",
min_value=1,
max_value=len(df),
value=min(10, len(df)))
# Display the table
st.subheader("Data Preview")
st.dataframe(df.head(n_rows))
### Data Visualization
st.subheader("Visualization")
# Variable selection from table columns
var = st.selectbox("Select first variable", options=df.columns)
# Let's keep a constant variable of interest - in case of Titanic dataset, it's the survival rate!
# Make a histogram (distribution chart) if it is numeric variable
if df[var].dtype in ['float64', 'int64']:
st.subheader(f"Distribution of {var}")
fig = sns.histplot(data=df, x=var, hue='survived')
st.pyplot(fig.figure)
# Make a barchart if it is categorical variable
else:
st.subheader(f"Count plot of {var}")
fig = sns.countplot(data=df, x=var, hue='survived')
st.pyplot(fig.figure)
except Exception as e:
st.error(f"Error: {str(e)}")
st.write("Please check if the URL is valid and points to a CSV file.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?