Hey guys ,
I’m working on a sentiment analysis application with streamlit which includes checkboxes for user input to select an option , but if the user clicks on a checkbox the page refreshes and the result is not displayed on the screen. I have attached the code snippet to invoke the function for sentiment analysis which is implemented, I have also tried printing a string when the checkbox is ticked , but unfortunately that doesn’t work too.
Hey,
I checked the sentiment analysis code out but there is nothing that does this , I also tried the same logic with a selectbox , it works for the first choice but as soon as the second choice is clicked it does the same
I stick to my previous assessment. Unfortunately I don’t have your implementation of sentiment_analysis so I cannot be of much help.
The logic in the code you posted is correct as far as I can tell: when the checkbox is checked it will call sentiment_analysis(article_choice) and then write the return value.
This is the function to obtain the sentiment of a particular sentence
def sentiment_analysis(text):
sentiments = []
from nltk.sentiment import SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()
sentiment = sia.polarity_scores(text)
sentiments.append(sentiment)
for sentiment in sentiments:
return sentiment
This is how I display the sentiments
# Collect RSS feed URLs and keywords using the menu bar
rss_feeds = collect_rss_feeds()
keywords = collect_keywords()
fetch_btn= btn_holder.button("Fetch articles")
if fetch_btn :
# Parse RSS feeds and extract articles
count=0
print(count)
st.write("Articles containing keywords:")
articles = []
for rss_feed in rss_feeds:
parsed_feed = feedparser.parse(rss_feed)
for entry in parsed_feed.entries:
articles.append(entry.summary)
# Search for keywords in articles
keyword_articles = []
for keyword in keywords:
for article in articles:
if keyword in article:
keyword_articles.append(article)
article_choice= st.selectbox("select", keyword_articles)
if article_choice:
summarization_tab,sentiment_tab,wordcloud_tab= st.tabs(["Summarization", "Sentiment Analysis", "Word Cloud"])
with sentiment_tab:
st.subheader("Sentiment Analysis")
sentiment = sentiment_analysis(article_choice)
st.write(sentiment)
I ran your original code with this implementation of sentiment_analysis() and article_choice = "What's up, doc?" and got this when checked the checkbox:
the code that I have attached below also shows the same behaviour
import streamlit as st
def sentiment_analysis(text):
sentiment=[]
from nltk.sentiment import SentimentIntensityAnalyzer
sia= SentimentIntensityAnalyzer()
sentiment.append(sia.polarity_scores(text=text))
for sentiment in sentiment:
return sentiment
sentences= st.sidebar.text_input("enter some text seperated by commas")
sen= sentences.split(',')
if st.button("submit"):
st.subheader("Sentiment analysis")
choice=st.selectbox("sent", sen)
if choice != None:
print(choice)
sentiment= sentiment_analysis(text= choice)
st.write(sentiment)
The issue here is that when you change the choice in the selectbox, the app reruns. In the next run the submit button returns False and the code under the if does not run. So you can only see the polarity scores of the first sentence, but once you select another sentence the subheader, the selectbox and the scores dissapear.
To avoid this do the following:
Under the if clause, compute the values that you know you will need and store them in st.session_state. In this example you only need the list of sentences sen, in your actual code the list of articles keyword_articles.
Move the rest of the code outside the if so that it runs no matter if you pressed the button or not. Get the values that you need from st.session_state.
If you do just that, you will get a KeyError when you start a session because the values you need in st.session_state won’t be there yet. But if you write some sentences in the text_input and press the Submit button, you will be able to see the scores for each sentence.
In order to get rid of the KeyError, check if st.session_state has the values that you need before running the last part of the code.
import streamlit as st
def sentiment_analysis(text):
sentiment = []
from nltk.sentiment import SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()
sentiment.append(sia.polarity_scores(text=text))
for sentiment in sentiment:
return sentiment
sentences = st.sidebar.text_input("enter some text seperated by commas")
if st.button("submit"):
st.session_state["sen"] = sentences.split(",")
if "sen" not in st.session_state:
st.stop()
st.subheader("Sentiment analysis")
choice = st.selectbox("sent", st.session_state["sen"])
if choice != None:
print(choice)
sentiment = sentiment_analysis(text=choice)
st.write(sentiment)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.