Caching functions under a button


Whenever I am selecting an attribute from selectbox, the code runs again including the code under the button. I dont want the code under the button to rerun again, what can I do? I tried implementing session state but not sure where i am making mistake. Any help would be appreciated.

Code snippet:

    if st.session_state.get('button') != True:
        st.session_state['button'] = button
    if st.session_state['button'] == True and username:
            with st.spinner("Fetching⏳....."):
                tweets,lst = get_data(username,no_tweets)
                prompt, template = start_haystack(api_key,tweets)
                results = query(prompt, template)
                ans = results[0]
                st.markdown(f"<h4 style='font-family: Roboto; font-weight: normal;'>@{username} has been tweeting about:</h4>", unsafe_allow_html=True)
                st.markdown(f"<h4 style='font-family: Lato; border: 2px solid #26C3E2; padding: 15px; font-weight: normal;'>{ans}</h4>", unsafe_allow_html=True)
                st.markdown(f"<h3>Source Tweets:</h3>", unsafe_allow_html=True)
                for i in lst:
                    st.markdown(f"<h5 style='font-family: Chirp; font-weight: normal;'>{i}</h5>",unsafe_allow_html=True)
        except Exception as e:
                st.write("Kindly reduce the no. of tweets to summarize")

    if st.session_state['button']==True:
        keywords = get_keyword(api_key, ans)
        keys = Convert(keywords[0])
        st.markdown(f"<h3>Select a topic to know more</h3>", unsafe_allow_html=True)
        box = st.selectbox("", keys)
        for article in articles:
            st.write('Title:', article['title'])
            st.write('Link:', article['link'])
            st.write('Snippet:', article['snippet'])
    elif button and not username:
        st.warning("Please enter a twitter handle")

@OjasMittal Hi,

You might be interested to take a look into the add-on streamlit package from Mr-Milk that saves variables with create_store() in the session state that you can retrieve with the get_state() command.

In command prompt/Anaconda Powershell Prompt to install the package you can run in your virtual environment:

pip install fire-state

In your Python script add at top you can import the module:

from fire_state import create_store, form_update, get_state, set_state, get_store, set_store
# create in session state a slot with key-value pair
create_store("MY_SLOT_NAME", [("my_btn", False)])

# define button
button = st.button("Know")

# update value in session state to value equal of button e.g. True if clicked
set_state("MY_SLOT_NAME", ("my_btn", button))

# set condition to only code run if button is not True
if get_state("MY_SLOT_NAME", "my_btn") != True:
  st.write(do not run this!)

Hope that helps! If not would you be able to create a dummy example that I can run as I do not fully understand what you are trying to accomplish as your code has dependencies not included in the code not important for solving this question.

Thanks for the information

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.