How to update selection box value based on another selection box in Streamlit app

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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}")
</code>
<code>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}") </code>
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}")

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật