Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,6 +23,7 @@ if "trending_top_n" not in st.session_state:
|
|
| 23 |
if "change_top_n" not in st.session_state:
|
| 24 |
st.session_state.change_top_n = DEFAULT_TOP_N
|
| 25 |
|
|
|
|
| 26 |
#############################
|
| 27 |
# PAGE 1: Historical Sentiment
|
| 28 |
#############################
|
|
@@ -106,7 +107,14 @@ def plot_dual_axes(
|
|
| 106 |
return fig
|
| 107 |
|
| 108 |
def run_historical_sentiment(ticker):
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
df = get_historical_sentiment(ticker)
|
| 111 |
if df.empty:
|
| 112 |
st.error("No data found for " + ticker)
|
|
@@ -115,7 +123,7 @@ def run_historical_sentiment(ticker):
|
|
| 115 |
common_range = [df["date"].min(), df["date"].max()]
|
| 116 |
|
| 117 |
st.subheader(f"{ticker} Sentiment Scores")
|
| 118 |
-
st.write("Compares
|
| 119 |
fig1 = plot_dual_axes(
|
| 120 |
df, "date", "stocktwitsSentiment", "twitterSentiment",
|
| 121 |
f"{ticker} Sentiment Scores",
|
|
@@ -125,7 +133,7 @@ def run_historical_sentiment(ticker):
|
|
| 125 |
st.plotly_chart(fig1, use_container_width=True)
|
| 126 |
|
| 127 |
st.subheader(f"{ticker} Posts")
|
| 128 |
-
st.write("
|
| 129 |
fig2 = plot_dual_axes(
|
| 130 |
df, "date", "stocktwitsPosts", "twitterPosts",
|
| 131 |
f"{ticker} Posts",
|
|
@@ -135,7 +143,7 @@ def run_historical_sentiment(ticker):
|
|
| 135 |
st.plotly_chart(fig2, use_container_width=True)
|
| 136 |
|
| 137 |
st.subheader(f"{ticker} Comments")
|
| 138 |
-
st.write("
|
| 139 |
fig3 = plot_dual_axes(
|
| 140 |
df, "date", "stocktwitsComments", "twitterComments",
|
| 141 |
f"{ticker} Comments",
|
|
@@ -145,7 +153,7 @@ def run_historical_sentiment(ticker):
|
|
| 145 |
st.plotly_chart(fig3, use_container_width=True)
|
| 146 |
|
| 147 |
st.subheader(f"{ticker} Likes")
|
| 148 |
-
st.write("Compares
|
| 149 |
fig4 = plot_dual_axes(
|
| 150 |
df, "date", "stocktwitsLikes", "twitterLikes",
|
| 151 |
f"{ticker} Likes",
|
|
@@ -155,7 +163,7 @@ def run_historical_sentiment(ticker):
|
|
| 155 |
st.plotly_chart(fig4, use_container_width=True)
|
| 156 |
|
| 157 |
st.subheader(f"{ticker} Impressions")
|
| 158 |
-
st.write("
|
| 159 |
fig5 = plot_dual_axes(
|
| 160 |
df, "date", "stocktwitsImpressions", "twitterImpressions",
|
| 161 |
f"{ticker} Impressions",
|
|
@@ -164,6 +172,7 @@ def run_historical_sentiment(ticker):
|
|
| 164 |
)
|
| 165 |
st.plotly_chart(fig5, use_container_width=True)
|
| 166 |
|
|
|
|
| 167 |
#############################
|
| 168 |
# PAGE 2: Trending Sentiment
|
| 169 |
#############################
|
|
@@ -249,15 +258,24 @@ def plot_trending(df, title, top_n):
|
|
| 249 |
st.plotly_chart(fig, use_container_width=True)
|
| 250 |
|
| 251 |
def run_trending_sentiment(top_n):
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
st.write("## Bullish Trending")
|
|
|
|
| 254 |
bullish_df = fetch_trending_sentiment(sentiment_type="bullish", source="stocktwits")
|
| 255 |
plot_trending(bullish_df, title="Bullish Trending", top_n=top_n)
|
| 256 |
|
| 257 |
st.write("## Bearish Trending")
|
|
|
|
| 258 |
bearish_df = fetch_trending_sentiment(sentiment_type="bearish", source="stocktwits")
|
| 259 |
plot_trending(bearish_df, title="Bearish Trending", top_n=top_n)
|
| 260 |
|
|
|
|
| 261 |
#############################
|
| 262 |
# PAGE 3: Sentiment Change
|
| 263 |
#############################
|
|
@@ -343,15 +361,24 @@ def plot_change_sentiment(df, title, top_n):
|
|
| 343 |
st.plotly_chart(fig, use_container_width=True)
|
| 344 |
|
| 345 |
def run_change_sentiment(top_n):
|
| 346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
st.write("## Bullish Sentiment Change")
|
|
|
|
| 348 |
bullish_df = fetch_change_sentiment(sentiment_type="bullish", source="stocktwits")
|
| 349 |
plot_change_sentiment(bullish_df, title="Bullish Sentiment Change", top_n=top_n)
|
| 350 |
|
| 351 |
st.write("## Bearish Sentiment Change")
|
|
|
|
| 352 |
bearish_df = fetch_change_sentiment(sentiment_type="bearish", source="stocktwits")
|
| 353 |
plot_change_sentiment(bearish_df, title="Bearish Sentiment Change", top_n=top_n)
|
| 354 |
|
|
|
|
| 355 |
#############################
|
| 356 |
# MAIN APP
|
| 357 |
#############################
|
|
@@ -359,23 +386,25 @@ def run_change_sentiment(top_n):
|
|
| 359 |
def main():
|
| 360 |
st.set_page_config(page_title="Social Sentiment Analysis", layout="wide")
|
| 361 |
st.title("Trending Social Sentiment")
|
|
|
|
|
|
|
| 362 |
st.write(
|
| 363 |
-
"
|
| 364 |
-
"
|
| 365 |
)
|
| 366 |
|
| 367 |
with st.sidebar.expander("Navigation and Options", expanded=True):
|
| 368 |
page = st.radio(
|
| 369 |
"Select Analysis Page",
|
| 370 |
("Historical Sentiment", "Trending Sentiment", "Sentiment Change"),
|
| 371 |
-
help="Choose
|
| 372 |
)
|
| 373 |
|
| 374 |
if page == "Historical Sentiment":
|
| 375 |
ticker = st.text_input(
|
| 376 |
"Ticker Symbol",
|
| 377 |
value=st.session_state.historical_ticker,
|
| 378 |
-
help="Enter a ticker symbol."
|
| 379 |
)
|
| 380 |
if st.button("Run Historical Sentiment Analysis"):
|
| 381 |
st.session_state.historical_run = True
|
|
@@ -410,21 +439,21 @@ def main():
|
|
| 410 |
if st.session_state.historical_run:
|
| 411 |
run_historical_sentiment(st.session_state.historical_ticker)
|
| 412 |
else:
|
| 413 |
-
st.info("
|
| 414 |
|
| 415 |
elif page == "Trending Sentiment":
|
| 416 |
st.header("Trending Social Sentiment")
|
| 417 |
if st.session_state.trending_run:
|
| 418 |
run_trending_sentiment(st.session_state.trending_top_n)
|
| 419 |
else:
|
| 420 |
-
st.info("
|
| 421 |
|
| 422 |
elif page == "Sentiment Change":
|
| 423 |
st.header("Social Sentiment Change")
|
| 424 |
if st.session_state.change_run:
|
| 425 |
run_change_sentiment(st.session_state.change_top_n)
|
| 426 |
else:
|
| 427 |
-
st.info("
|
| 428 |
|
| 429 |
if __name__ == "__main__":
|
| 430 |
main()
|
|
|
|
| 23 |
if "change_top_n" not in st.session_state:
|
| 24 |
st.session_state.change_top_n = DEFAULT_TOP_N
|
| 25 |
|
| 26 |
+
|
| 27 |
#############################
|
| 28 |
# PAGE 1: Historical Sentiment
|
| 29 |
#############################
|
|
|
|
| 107 |
return fig
|
| 108 |
|
| 109 |
def run_historical_sentiment(ticker):
|
| 110 |
+
# Explanation of this page
|
| 111 |
+
st.write("**Historical Sentiment**")
|
| 112 |
+
st.write(
|
| 113 |
+
"Use this page to see how sentiment changes over time for a specific ticker. "
|
| 114 |
+
"It retrieves past data on sentiment, posts, comments, likes, and impressions "
|
| 115 |
+
"from two social platforms. This can help spot long-term shifts or trends."
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
df = get_historical_sentiment(ticker)
|
| 119 |
if df.empty:
|
| 120 |
st.error("No data found for " + ticker)
|
|
|
|
| 123 |
common_range = [df["date"].min(), df["date"].max()]
|
| 124 |
|
| 125 |
st.subheader(f"{ticker} Sentiment Scores")
|
| 126 |
+
st.write("Compares sentiment scores from two social channels over the selected date range.")
|
| 127 |
fig1 = plot_dual_axes(
|
| 128 |
df, "date", "stocktwitsSentiment", "twitterSentiment",
|
| 129 |
f"{ticker} Sentiment Scores",
|
|
|
|
| 133 |
st.plotly_chart(fig1, use_container_width=True)
|
| 134 |
|
| 135 |
st.subheader(f"{ticker} Posts")
|
| 136 |
+
st.write("Tracks the number of posts on each platform. High volumes can hint at heightened interest.")
|
| 137 |
fig2 = plot_dual_axes(
|
| 138 |
df, "date", "stocktwitsPosts", "twitterPosts",
|
| 139 |
f"{ticker} Posts",
|
|
|
|
| 143 |
st.plotly_chart(fig2, use_container_width=True)
|
| 144 |
|
| 145 |
st.subheader(f"{ticker} Comments")
|
| 146 |
+
st.write("Shows how many comments occurred. This may reveal engagement depth.")
|
| 147 |
fig3 = plot_dual_axes(
|
| 148 |
df, "date", "stocktwitsComments", "twitterComments",
|
| 149 |
f"{ticker} Comments",
|
|
|
|
| 153 |
st.plotly_chart(fig3, use_container_width=True)
|
| 154 |
|
| 155 |
st.subheader(f"{ticker} Likes")
|
| 156 |
+
st.write("Compares the likes on each platform. More likes can signal positive reception.")
|
| 157 |
fig4 = plot_dual_axes(
|
| 158 |
df, "date", "stocktwitsLikes", "twitterLikes",
|
| 159 |
f"{ticker} Likes",
|
|
|
|
| 163 |
st.plotly_chart(fig4, use_container_width=True)
|
| 164 |
|
| 165 |
st.subheader(f"{ticker} Impressions")
|
| 166 |
+
st.write("Shows overall impressions. Higher impressions may reflect broader visibility.")
|
| 167 |
fig5 = plot_dual_axes(
|
| 168 |
df, "date", "stocktwitsImpressions", "twitterImpressions",
|
| 169 |
f"{ticker} Impressions",
|
|
|
|
| 172 |
)
|
| 173 |
st.plotly_chart(fig5, use_container_width=True)
|
| 174 |
|
| 175 |
+
|
| 176 |
#############################
|
| 177 |
# PAGE 2: Trending Sentiment
|
| 178 |
#############################
|
|
|
|
| 258 |
st.plotly_chart(fig, use_container_width=True)
|
| 259 |
|
| 260 |
def run_trending_sentiment(top_n):
|
| 261 |
+
# Explanation of this page
|
| 262 |
+
st.write("**Trending Sentiment**")
|
| 263 |
+
st.write(
|
| 264 |
+
"Use this page to see which symbols rank highest in bullish or bearish sentiment right now. "
|
| 265 |
+
"This helps pinpoint active tickers with strong positive or negative views."
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
st.write("## Bullish Trending")
|
| 269 |
+
st.write("Displays the top bullish tickers and their current sentiment.")
|
| 270 |
bullish_df = fetch_trending_sentiment(sentiment_type="bullish", source="stocktwits")
|
| 271 |
plot_trending(bullish_df, title="Bullish Trending", top_n=top_n)
|
| 272 |
|
| 273 |
st.write("## Bearish Trending")
|
| 274 |
+
st.write("Displays the top bearish tickers and their current sentiment.")
|
| 275 |
bearish_df = fetch_trending_sentiment(sentiment_type="bearish", source="stocktwits")
|
| 276 |
plot_trending(bearish_df, title="Bearish Trending", top_n=top_n)
|
| 277 |
|
| 278 |
+
|
| 279 |
#############################
|
| 280 |
# PAGE 3: Sentiment Change
|
| 281 |
#############################
|
|
|
|
| 361 |
st.plotly_chart(fig, use_container_width=True)
|
| 362 |
|
| 363 |
def run_change_sentiment(top_n):
|
| 364 |
+
# Explanation of this page
|
| 365 |
+
st.write("**Sentiment Change**")
|
| 366 |
+
st.write(
|
| 367 |
+
"Use this page to see if sentiment is trending up or down for each ticker. "
|
| 368 |
+
"Compare the current sentiment to how much it has shifted. This can reveal rapid changes."
|
| 369 |
+
)
|
| 370 |
+
|
| 371 |
st.write("## Bullish Sentiment Change")
|
| 372 |
+
st.write("Shows bullish sentiment and recent momentum shifts in that sentiment.")
|
| 373 |
bullish_df = fetch_change_sentiment(sentiment_type="bullish", source="stocktwits")
|
| 374 |
plot_change_sentiment(bullish_df, title="Bullish Sentiment Change", top_n=top_n)
|
| 375 |
|
| 376 |
st.write("## Bearish Sentiment Change")
|
| 377 |
+
st.write("Shows bearish sentiment and recent momentum shifts in that sentiment.")
|
| 378 |
bearish_df = fetch_change_sentiment(sentiment_type="bearish", source="stocktwits")
|
| 379 |
plot_change_sentiment(bearish_df, title="Bearish Sentiment Change", top_n=top_n)
|
| 380 |
|
| 381 |
+
|
| 382 |
#############################
|
| 383 |
# MAIN APP
|
| 384 |
#############################
|
|
|
|
| 386 |
def main():
|
| 387 |
st.set_page_config(page_title="Social Sentiment Analysis", layout="wide")
|
| 388 |
st.title("Trending Social Sentiment")
|
| 389 |
+
|
| 390 |
+
# Overall introduction to the app
|
| 391 |
st.write(
|
| 392 |
+
"Use this dashboard to explore historical, trending, and changing sentiment data. "
|
| 393 |
+
"You can research how mention counts and sentiments evolve for different symbols."
|
| 394 |
)
|
| 395 |
|
| 396 |
with st.sidebar.expander("Navigation and Options", expanded=True):
|
| 397 |
page = st.radio(
|
| 398 |
"Select Analysis Page",
|
| 399 |
("Historical Sentiment", "Trending Sentiment", "Sentiment Change"),
|
| 400 |
+
help="Choose which sentiment analysis you want."
|
| 401 |
)
|
| 402 |
|
| 403 |
if page == "Historical Sentiment":
|
| 404 |
ticker = st.text_input(
|
| 405 |
"Ticker Symbol",
|
| 406 |
value=st.session_state.historical_ticker,
|
| 407 |
+
help="Enter a ticker symbol for historical data."
|
| 408 |
)
|
| 409 |
if st.button("Run Historical Sentiment Analysis"):
|
| 410 |
st.session_state.historical_run = True
|
|
|
|
| 439 |
if st.session_state.historical_run:
|
| 440 |
run_historical_sentiment(st.session_state.historical_ticker)
|
| 441 |
else:
|
| 442 |
+
st.info("Enter a ticker and click the button.")
|
| 443 |
|
| 444 |
elif page == "Trending Sentiment":
|
| 445 |
st.header("Trending Social Sentiment")
|
| 446 |
if st.session_state.trending_run:
|
| 447 |
run_trending_sentiment(st.session_state.trending_top_n)
|
| 448 |
else:
|
| 449 |
+
st.info("Pick a top N and click the button.")
|
| 450 |
|
| 451 |
elif page == "Sentiment Change":
|
| 452 |
st.header("Social Sentiment Change")
|
| 453 |
if st.session_state.change_run:
|
| 454 |
run_change_sentiment(st.session_state.change_top_n)
|
| 455 |
else:
|
| 456 |
+
st.info("Pick a top N and click the button.")
|
| 457 |
|
| 458 |
if __name__ == "__main__":
|
| 459 |
main()
|