How to update selection box value based on another selection box

I am trying to dynamically change value of cluster_options inside cluster based on value of t selection box. But its not happening.

import streamlit as st
import wordcloud
import matplotlib.pyplot as plt
import pandas as pd

# Dummy implementation for illustration purposes
def words_from_data(filename, text_col, cluster):
    data = "example data for wordcloud generation"
    df = pd.DataFrame({"Sample Text": [f"Sample text {i}" for i in range(1, 101)]})
    llm_output = "Generated text from LLM."
    return data, df, llm_output

if 't' not in st.session_state:
    st.session_state.t = "P"

if 'cluster_options' not in st.session_state:
    st.session_state.cluster_options = [i for i in range(200)]

if __name__ == '__main__':
    st.title("WordCloud Generator")
    st.write("Hello! This web app allows you to generate a word cloud from the data of your choice. Enjoy!")

    # Sidebar widgets
    filename = st.sidebar.text_input("Give the complete location of the Excel file")
    text_col = st.sidebar.text_input("Give the Text column name")

    t = st.sidebar.selectbox("Choose the Cluster Type.", ["P", "V"], index=["P", "V"].index(st.session_state.t))

    if t != st.session_state.t:
        st.session_state.t = t
        if t == "P":
            st.session_state.cluster_options = [i for i in range(200)]
        else:
            st.session_state.cluster_options = [i for i in range(300)]
        st.experimental_rerun()

    cluster = st.sidebar.selectbox("Choose the Cluster Number.", st.session_state.cluster_options)

    samples = st.sidebar.selectbox("Choose the number of Samples you want to show.", [i for i in range(5, 50, 5)])
    background = st.sidebar.selectbox("Choose the background color for your wordcloud.", ["black", "white"])
    style = st.sidebar.selectbox("Choose a wordcloud style.", [
        'viridis', 'plasma', 'inferno', 'magma', 'cividis', 'Pastel1', 'Pastel2', 'Paired', 
        'Accent', 'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', 'rainbow', 
        'jet', 'turbo', 'gray', 'bone', 'pink', 'spring', 'summer', 'autumn', 'winter', 
        'cool', 'hot', 'copper'
    ])
    submit = st.sidebar.button('Generate WordCloud') 

    # Configuring applet response to form submission
    if submit:
        with st.spinner("Please wait while your wordcloud is being generated..."):
            try:
                data, df, llm_output = words_from_data(filename, text_col, cluster)
                cloud = wordcloud.WordCloud(
                    background_color=background, colormap=style, width=1500, height=1000
                ).generate(data)
                fig = plt.figure(figsize=(15, 10))  # Adjust figsize as needed
                plt.imshow(cloud)
                plt.axis("off")
                st.header(f"Wordcloud from the Cluster {cluster} data")
                st.pyplot(fig)
                st.write(llm_output)
                st.write("To create more wordclouds, configure the settings on the sidebar and click the 'Generate Wordcloud' button.")

                # Display text samples as table
                st.subheader("Text Samples")
                st.write(df.head(samples))  # Display first 'samples' number of samples
                st.write("To create more wordclouds, configure the settings on the sidebar and click the 'Generate Wordcloud' button.")
            except Exception as e:
                st.header(f"There has been an error in fetching the data: {e}")
1 Like