hblim commited on
Commit
ba6928c
Β·
1 Parent(s): ebee14c

modified plot and added intro text

Browse files
Files changed (1) hide show
  1. frontend/app.py +33 -22
frontend/app.py CHANGED
@@ -15,8 +15,21 @@ from data_utils import (
15
  )
16
  from text_analysis import keywords_for_df
17
 
 
18
  st.title("Reddit Sentiment Monitor")
19
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # ── Load & transform data ────────────────────────────────────────────────────
22
  df = load_summary()
@@ -46,13 +59,14 @@ date_range = st.date_input(
46
  start_date, end_date = date_range
47
  filtered_df = df[(df["date"].dt.date >= start_date) & (df["date"].dt.date <= end_date)]
48
 
49
- # Add a multiselect widget for choosing which subreddits to display
50
- selected_subs = st.multiselect(
51
- "Select subreddits to display",
 
52
  options=list(subreddits),
53
- default=list(subreddits)
54
  )
55
- plot_df = filtered_df[filtered_df["subreddit"].isin(selected_subs)]
56
 
57
  # Define hover selection for nearest point
58
  nearest = alt.selection_single(
@@ -63,30 +77,23 @@ nearest = alt.selection_single(
63
  empty="none"
64
  )
65
 
66
- # Base chart for DRY encoding
67
  base = alt.Chart(plot_df).encode(
68
  x=alt.X("date:T", title="Date", axis=alt.Axis(format=time_format)),
69
- y=alt.Y("community_weighted_sentiment:Q", title="Community Weighted Sentiment"),
70
- color=alt.Color(
71
- "subreddit:N",
72
- scale=alt.Scale(domain=list(subreddits), range=list(subreddit_colors.values())),
73
- legend=alt.Legend(
74
- title="Subreddit",
75
- orient="top",
76
- direction="vertical",
77
- columns=1
78
- )
79
- )
80
  )
81
 
82
- # Draw lines
83
- line = base.mark_line()
 
 
 
84
 
85
  # Invisible selectors to capture hover events
86
  selectors = base.mark_point(opacity=0).add_selection(nearest)
87
 
88
  # Draw highlighted points on hover
89
- points_hover = base.mark_point(size=60).encode(
90
  opacity=alt.condition(nearest, alt.value(1), alt.value(0))
91
  )
92
 
@@ -99,9 +106,13 @@ tooltips = base.mark_rule(color="gray").encode(
99
  ]
100
  ).transform_filter(nearest)
101
 
102
- # Layer everything and make interactive
103
  hover_chart = alt.layer(line, selectors, points_hover, tooltips).properties(
104
- height=300
 
 
 
 
105
  ).interactive()
106
 
107
  st.altair_chart(hover_chart, use_container_width=True)
 
15
  )
16
  from text_analysis import keywords_for_df
17
 
18
+
19
  st.title("Reddit Sentiment Monitor")
20
 
21
+ st.markdown(
22
+ """
23
+ **Welcome!** This page shows how Reddit's AI communities feel day-to-day.
24
+
25
+ β€’ A daily pipeline grabs new posts and comments, scores their tone with a sentiment model, and saves the totals to a public HuggingFace [dataset](https://huggingface.co/datasets/hblim/top_reddit_posts_daily). \n
26
+ β€’ The line chart below plots *community-weighted sentiment*: each post/comment's sentiment is scaled by its upvotes so busier discussions matter more. Values run from βˆ’1 (negative) to +1 (positive). \n
27
+ β€’ The table further down lets you drill into the posts that shaped the mood on a chosen date. \n\n
28
+
29
+ Pick a subreddit and explore!
30
+ """
31
+ )
32
+
33
 
34
  # ── Load & transform data ────────────────────────────────────────────────────
35
  df = load_summary()
 
59
  start_date, end_date = date_range
60
  filtered_df = df[(df["date"].dt.date >= start_date) & (df["date"].dt.date <= end_date)]
61
 
62
+ # Add a dropdown (selectbox) for choosing a single subreddit to display
63
+ default_sub = "artificial" if "artificial" in subreddits else list(subreddits)[0]
64
+ selected_subreddit = st.selectbox(
65
+ "Select subreddit",
66
  options=list(subreddits),
67
+ index=list(subreddits).index(default_sub)
68
  )
69
+ plot_df = filtered_df[filtered_df["subreddit"] == selected_subreddit]
70
 
71
  # Define hover selection for nearest point
72
  nearest = alt.selection_single(
 
77
  empty="none"
78
  )
79
 
80
+ # Base chart for DRY encoding (single subreddit, constant colour)
81
  base = alt.Chart(plot_df).encode(
82
  x=alt.X("date:T", title="Date", axis=alt.Axis(format=time_format)),
83
+ y=alt.Y("community_weighted_sentiment:Q", title="Community Weighted Sentiment")
 
 
 
 
 
 
 
 
 
 
84
  )
85
 
86
+ # Determine colour for the chosen subreddit
87
+ line_colour = subreddit_colors.get(selected_subreddit, "#1f77b4")
88
+
89
+ # Draw line for the selected subreddit
90
+ line = base.mark_line(color=line_colour)
91
 
92
  # Invisible selectors to capture hover events
93
  selectors = base.mark_point(opacity=0).add_selection(nearest)
94
 
95
  # Draw highlighted points on hover
96
+ points_hover = base.mark_point(size=60, color=line_colour).encode(
97
  opacity=alt.condition(nearest, alt.value(1), alt.value(0))
98
  )
99
 
 
106
  ]
107
  ).transform_filter(nearest)
108
 
109
+ # Layer everything and make interactive, with title showing subreddit
110
  hover_chart = alt.layer(line, selectors, points_hover, tooltips).properties(
111
+ height=300,
112
+ title=alt.TitleParams(
113
+ text=f"Daily Community Weighted Sentiment for {selected_subreddit}",
114
+ offset=20 # adds space above the title so it is not cut off
115
+ )
116
  ).interactive()
117
 
118
  st.altair_chart(hover_chart, use_container_width=True)