import streamlit as st st.set_page_config( page_title="Social Media Sentiment Analyzer", page_icon="📊", layout="wide" ) import pandas as pd import helper_functions as hf import plotly.express as px import plotly.io as pio import plotly # Whenever the search button is clicked, the search_callback function is called def search_callback(): if twitter_agree: if len(st.session_state.search_term) == 0: st.error("Please enter a search term") return st.session_state.df = hf.get_tweets(st.session_state.search_term, st.session_state.num_tweets) st.session_state.df = hf.get_sentiment(st.session_state.df) def twitter_form(): with st.form(key="search_form"): st.subheader("Search Parameters") st.text_input("Enter a User handle (like _@elonmusk_), Hashtag (like _#Bitcoin_) or Topic (like _climate change_)", key="search_term") st.slider("Number of tweets", min_value=100, max_value=500, key="num_tweets") st.form_submit_button(label="Search", on_click=search_callback) st.markdown( "Note: it may take a while to load the results, especially with large number of tweets" ) with st.sidebar: st.title("Social Media Sentiment Analyzer") st.subheader("Choose your platform") twitter_agree = st.checkbox('Twitter') if twitter_agree: twitter_form() st.markdown( "
Created by Taaha Bajwa
", unsafe_allow_html=True, ) if "df" in st.session_state: def make_dashboard(tweet_df, bar_color, wc_color): # first row col1, col2, col3 = st.columns([28, 34, 38]) with col1: sentiment_plot = hf.plot_sentiment(tweet_df) sentiment_plot.update_layout(height=350, title_x=0.5) st.plotly_chart(sentiment_plot, theme=None, use_container_width=True) with col2: top_unigram = hf.get_top_n_gram(tweet_df, ngram_range=(1, 1), n=10) unigram_plot = hf.plot_n_gram( top_unigram, title="Top 10 Occuring Words", color=bar_color ) unigram_plot.update_layout(height=350) st.plotly_chart(unigram_plot, theme=None, use_container_width=True) with col3: top_bigram = hf.get_top_n_gram(tweet_df, ngram_range=(2, 2), n=10) bigram_plot = hf.plot_n_gram( top_bigram, title="Top 10 Occuring Bigrams", color=bar_color ) bigram_plot.update_layout(height=350) st.plotly_chart(bigram_plot, theme=None, use_container_width=True) # second row col1, col2 = st.columns([60, 40]) with col1: def sentiment_color(sentiment): if sentiment == "Positive": return "background-color: #54A24B; color: white" elif sentiment == "Negative": return "background-color: #FF7F0E" else: return "background-color: #1F77B4" st.dataframe( tweet_df[["Sentiment", "Tweet"]].style.applymap( sentiment_color, subset=["Sentiment"] ), height=350, ) with col2: wordcloud = hf.plot_wordcloud(tweet_df, colormap=wc_color) st.pyplot(wordcloud) adjust_tab_font = """ """ st.write(adjust_tab_font, unsafe_allow_html=True) tab1, tab2, tab3, tab4 = st.tabs(["All", "Positive 😊", "Negative ☚ī¸", "Neutral 😐"]) with tab1: tweet_df = st.session_state.df make_dashboard(tweet_df, bar_color="#1F77B4", wc_color="Blues") with tab2: tweet_df = st.session_state.df.query("Sentiment == 'Positive'") make_dashboard(tweet_df, bar_color="#54A24B", wc_color="Greens") with tab3: tweet_df = st.session_state.df.query("Sentiment == 'Negative'") make_dashboard(tweet_df, bar_color="#FF7F0E", wc_color="Oranges") with tab4: tweet_df = st.session_state.df.query("Sentiment == 'Neutral'") make_dashboard(tweet_df, bar_color="#1F77B4", wc_color="Blues")