fix(dashboard): enabled scraper access without existing data

This commit is contained in:
Sanjeev Kumar 2025-12-14 07:52:23 +05:30
parent 8ce832010f
commit 604a50ce17

View file

@ -126,14 +126,15 @@ def main():
empty_msg = "No scraped subreddits found." empty_msg = "No scraped subreddits found."
icon = "📁" icon = "📁"
selected_sub = None
if not options: if not options:
st.sidebar.warning(empty_msg) st.sidebar.warning(empty_msg)
if source_type == "Subreddits": if source_type == "Subreddits":
st.sidebar.code("python main.py <sub_name> ...") st.sidebar.info("Go to '⚙️ Scraper' tab to start scraping.")
else:
st.sidebar.info("Go to '⚙️ Scraper' tab to start scraping users.")
else: else:
st.sidebar.code("python main.py <user> --user ...")
return # Stop execution if no data for selected type
# Selector # Selector
selected_sub = st.sidebar.selectbox( selected_sub = st.sidebar.selectbox(
f"Select {source_type[:-1]}", # "Select Subreddit" or "Select User" f"Select {source_type[:-1]}", # "Select Subreddit" or "Select User"
@ -141,24 +142,42 @@ def main():
format_func=lambda x: f"{icon} {x[2:] if x.startswith(('r_', 'u_')) else x}" format_func=lambda x: f"{icon} {x[2:] if x.startswith(('r_', 'u_')) else x}"
) )
# Load data # Load data if selected
posts_df = pd.DataFrame()
comments_df = pd.DataFrame()
data_loaded = False
if selected_sub:
data_dir = Path(__file__).parent.parent / 'data' data_dir = Path(__file__).parent.parent / 'data'
sub_path = data_dir / selected_sub sub_path = data_dir / selected_sub
data = load_subreddit_data(sub_path) data = load_subreddit_data(sub_path)
if 'posts' not in data: if 'posts' in data:
st.error("No posts data found!")
return
posts_df = data['posts'] posts_df = data['posts']
comments_df = data.get('comments', pd.DataFrame()) comments_df = data.get('comments', pd.DataFrame())
data_loaded = True
else:
st.error("No posts data found for selected item!")
# Main content tabs # Define Tabs
tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs([ # Data tabs only if data loaded
"📊 Overview", "📈 Analytics", "🔍 Search", "💬 Comments", "⚙️ Scraper", "📋 Job History", "🔌 Integrations" tab_list = []
]) if data_loaded:
tab_list.extend(["📊 Overview", "📈 Analytics", "🔍 Search", "💬 Comments"])
with tab1: # Always present tabs
tab_list.extend(["⚙️ Scraper", "📋 Job History", "🔌 Integrations"])
# Create tabs
tabs = st.tabs(tab_list)
# Map tabs to variables for easy access
tab_map = {name: tabs[i] for i, name in enumerate(tab_list)}
# --- RENDER TABS ---
if data_loaded:
with tab_map["📊 Overview"]:
st.header(f"📊 Overview: {selected_sub}") st.header(f"📊 Overview: {selected_sub}")
# Metrics row # Metrics row
@ -204,7 +223,7 @@ def main():
top_posts = posts_df.nlargest(10, 'score')[['title', 'score', 'num_comments', 'post_type', 'created_utc']] top_posts = posts_df.nlargest(10, 'score')[['title', 'score', 'num_comments', 'post_type', 'created_utc']]
st.dataframe(top_posts) st.dataframe(top_posts)
with tab2: with tab_map["📈 Analytics"]:
st.header("📈 Analytics") st.header("📈 Analytics")
# Sentiment Analysis # Sentiment Analysis
@ -259,7 +278,7 @@ def main():
for day, avg_score in timing_data['best_days']: for day, avg_score in timing_data['best_days']:
st.write(f"{day} - Avg Score: {avg_score:.1f}") st.write(f"{day} - Avg Score: {avg_score:.1f}")
with tab3: with tab_map["🔍 Search"]:
st.header("🔍 Search Posts") st.header("🔍 Search Posts")
# Search form # Search form
@ -310,7 +329,7 @@ def main():
st.write(f"Found {len(filtered)} results") st.write(f"Found {len(filtered)} results")
st.dataframe(filtered[['title', 'score', 'num_comments', 'post_type', 'author', 'created_utc']].head(50)) st.dataframe(filtered[['title', 'score', 'num_comments', 'post_type', 'author', 'created_utc']].head(50))
with tab4: with tab_map["💬 Comments"]:
st.header("💬 Comments Analysis") st.header("💬 Comments Analysis")
if len(comments_df) == 0: if len(comments_df) == 0:
@ -345,7 +364,9 @@ def main():
top_authors = comments_df['author'].value_counts().head(10) top_authors = comments_df['author'].value_counts().head(10)
st.bar_chart(top_authors) st.bar_chart(top_authors)
with tab5: # Scraper Tab (Always visible)
with tab_map["⚙️ Scraper"]:
st.header("⚙️ Scraper Controls") st.header("⚙️ Scraper Controls")
# Persistence logic # Persistence logic