Ashar086's picture
Update app.py
e8548a4 verified
import streamlit as st
import pandas as pd
import plotly.express as px
import seaborn as sns
import matplotlib.pyplot as plt
import time
from wordcloud import WordCloud
# Load Data
df = pd.read_csv('Data/Time-Wasters on Social Media.csv')
# Custom CSS Styling
def local_css():
st.markdown("""
<style>
.main {background-color: #f5f7fa;}
h1 {color: #003366; text-align: center;}
h3 {color: #666666; text-align: center;}
.stButton>button {background-color: #003366; color: white; font-size: 18px;}
</style>
""", unsafe_allow_html=True)
local_css()
# Sidebar Navigation
st.sidebar.image('assets/logo.png', width=200)
st.sidebar.title("Navigation")
page = st.sidebar.radio("Go to", ['Home', 'Time Wasters', 'Engagement Levels', 'Addiction Levels'])
# Session Timer (Tracks time spent on dashboard)
start_time = time.time()
if 'start_time' not in st.session_state:
st.session_state['start_time'] = start_time
elapsed_time = time.time() - st.session_state['start_time']
st.sidebar.metric("Time Spent Here", f"{int(elapsed_time)} sec")
# Home Page
if page == 'Home':
st.title("📊 Welcome to the Time-Wasters Analytics Dashboard")
st.markdown("""
### What you will explore:
1. **Time-Wasting Trends on Social Media**
2. **Engagement Levels & Productivity Loss**
3. **Social Media Addiction Insights**
""")
st.image('assets/dashboard_preview.png')
# Time Wasters Analysis
elif page == 'Time Wasters':
st.title("📱 Time Wasters on Social Media")
col1, col2 = st.columns(2)
col1.metric("Total Users", len(df['UserID'].unique()))
col2.metric("Total Time Spent", int(df['Total Time Spent'].sum()))
# Filters
selected_country = st.selectbox("Select Country", df['Location'].unique())
selected_gender = st.selectbox("Select Gender", df['Gender'].unique())
selected_platform = st.selectbox("Select Platform", df['Platform'].unique())
age_range = st.slider("Select Age Range", int(df['Age'].min()), int(df['Age'].max()), (20, 40))
# Filter Data
filtered_data = df[(df['Location'] == selected_country) &
(df['Gender'] == selected_gender) &
(df['Age'].between(*age_range)) &
(df['Platform'] == selected_platform)]
avg_addiction_level = filtered_data['Addiction Level'].mean()
st.subheader(f"Average Addiction Level: {avg_addiction_level:.2f}")
# Animated Bar Chart
fig = px.histogram(filtered_data, x='Addiction Level', nbins=10, color_discrete_sequence=['teal'], animation_frame='Age')
st.plotly_chart(fig)
# Engagement Levels
elif page == 'Engagement Levels':
st.title("🎯 Engagement Levels on Social Media")
selected_country = st.selectbox("Select Country", df['Location'].unique(), key='engage_country')
selected_platform = st.selectbox("Select Platform", df['Platform'].unique(), key='engage_platform')
filtered_data = df[(df['Location'] == selected_country) & (df['Platform'] == selected_platform)]
avg_engagement = filtered_data['Engagement'].mean()
st.subheader(f"Average Engagement: {avg_engagement:.2f}")
# Word Cloud of Watch Reasons
text = ' '.join(filtered_data['Watch Reason'].dropna())
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)
st.image(wordcloud.to_array())
# Engagement Scatter Plot
fig = px.scatter(filtered_data, x='Engagement', y='ProductivityLoss', color='Platform', size='Total Time Spent')
st.plotly_chart(fig)
# Addiction Levels
elif page == 'Addiction Levels':
st.title("⚠️ Social Media Addiction Levels")
selected_country = st.selectbox("Select Country", df['Location'].unique(), key='addict_country')
selected_platform = st.selectbox("Select Platform", df['Platform'].unique(), key='addict_platform')
filtered_data = df[(df['Location'] == selected_country) & (df['Platform'] == selected_platform)]
avg_addiction = filtered_data['Addiction Level'].mean()
st.subheader(f"Average Addiction Level: {avg_addiction:.2f}")
# Addiction vs Age Line Chart
fig, ax = plt.subplots()
sns.lineplot(data=filtered_data, x='Age', y='Addiction Level', marker='o', ax=ax)
ax.set_title("Addiction Level vs Age")
st.pyplot(fig)
# Heatmap
st.subheader("Engagement & Addiction Heatmap")
heatmap_data = df.pivot_table(index='Location', columns='Platform', values='Addiction Level', aggfunc='mean')
fig, ax = plt.subplots(figsize=(10,6))
sns.heatmap(heatmap_data, cmap='coolwarm', annot=True, ax=ax)
st.pyplot(fig)
st.sidebar.write("© 2025 Social Media Analytics Dashboard")