Need help with selectbox and displaying information after selectbox is clicked

I need help in understanding how to add text amongst other things once I’ve selected something in my st.selectbox

For instance once one of the options are clicked on the select box, I want to display more information such as text, a frequency distribution, and wordcloud image.

How would I go about doing this?

Hi @razahuss, welcome to the Streamlit community!! :wave: :partying_face:

The st.multiselect widget returns a list containing options selected by the user. To condition an action on the options from the returned list, you can check if the option is present in the list and, if so, perform the action.

Here’s an example. Let’s say we want the user to provide one or more forms of contact information so that we can reach out to them. The user can choose Email, Home phone, and/or Mobile phone from st.multiselect.

  • If the user selects Email, we want to show them a text input where they can enter their email.
    • If they enter their email, we want to display the email to them.

Here’s one way you could write the corresponding code:

import streamlit as st

option = st.multiselect(
    'How would you like to be contacted?',
    ['Email', 'Home phone', 'Mobile phone'])

if 'Email' in option: # If user selects Email  do 👇
    email_id = st.text_input('Enter the email address we should contact: ')
    if email_id: # If user enters email, do 👇
        st.write(f'Please check {email_id} for an email from us!')



Happy Streamlit’ing! :balloon:

Hi @snehankekre , thank you for your response.

I understand using the if method, but that can be pretty redundant when going through 27 topics. So I added the text, but I also need to make sure the freqdist is being added as well, all which depends on which topic was selected.
I’m not sure if adding a for loop or maybe filtering the topics would solve that.

I also have an issue displaying the data for my linechart to display the freqdist.

st.header("Part A - The Top Stories API")
st.markdown("This app uses the Top Stories API to display the most common words used in the top current \
        articles based on a specified topic selected by the user. The data is displayed as line chart \
        and as a wordcloud image.")

st.subheader("I - Topic Selection")
name = st.text_input("Please enter your name")
topic = st.selectbox(
    "Select a topic of your interest",
    ["arts", "automobiles", "books", "business", "fashion", "food", "health", "home",
     "insider", "magazine", "movies", "nyregion", "obituaries", "opinion", "politics",
     "realestate", "science", "sports", "sundayreview", "technology", "theater", "t-magazine",
     "travel", "upshot", "us", "world"]

if 'automobiles' in topic:
    st.write("Hi " + name + ", you selected the " + topic + " topic.")

    url = "" + topic + ".json?api-key=" + api_key
    response = requests.get(url).json()

    main_functions.save_to_file(response, "JSON_Files/response.json")
    my_articles = main_functions.read_from_file("JSON_Files/response.json")
    str1 = ""

    for i in my_articles["results"]:
        str1 = str1 + i["abstract"]

    sentences = sent_tokenize(str1)
    words = word_tokenize(str1)

    words_no_punc = []

    for w in words:
        if w.isalpha():

    stopwords = stopwords.words("english")

    clean_words = []
    for w in words_no_punc:
        if w not in stopwords:

    fdist = FreqDist(clean_words).most_common(10)

    st.subheader("II - Frequency Distribution")
    if st.checkbox("Click here to generate frequency distribution"):
        chart_data = pd.DataFrame(
            np.random.rand(20, 2),
            columns=['Words', 'Count']

    st.subheader("III - WordCloud")
    if st.checkbox("Click here to generate wordcloud"):
        wordcloud = WordCloud().generate(str1)
        plt.figure(figsize=(12, 12))
        st.set_option('deprecation.showPyplotGlobalUse', False)
        st.markdown("<p style='text-align: center;'>Wordcloud generated for " + topic + " topic.",