Streamlit rerun problem!

Iā€™ve been experimenting with streamlit recently and I run into a problemā€¦
I know that everytime you click on an UI element, streamlit rerun the whole script, here is a recoding script, what I donā€™t understand is, after I recorded something, the second time when I click on other UI elements, the previous recording will show up, even if I set the variables to None, itā€™s really puzzling, some help are appreciatedā€¦

            mp3_filename = None
            audio = None
            
            # Add the audiorecorder component to the container
            audio = audiorecorder()      

            if "prev_selected_topic" not in st.session_state:
                st.session_state.prev_selected_topic = selected_topic
            if "prev_selected_index" not in st.session_state:
                st.session_state.prev_selected_index = selected_index

            if st.session_state.prev_selected_topic != selected_topic:
                audio = None
                mp3_filename = None
                st.session_state.prev_selected_topic = selected_topic

            if st.session_state.prev_selected_index != selected_index:
                audio = None
                mp3_filename = None
                st.session_state.prev_selected_index = selected_index

            if audio is not None and len(audio) > 0:
                mp3_filename = process_audio(audio)

            if mp3_filename is not None and os.path.exists(mp3_filename):
                with open(mp3_filename, 'rb') as f:
                    mp3_audio = f.read()
                st.audio(mp3_audio, format="audio/wav")

Could you please give a complete script which reproduces this issue? Itā€™s a bit hard to be sure whatā€™s going on because your script isnā€™t useable as-is. If you could create a complete example that simplifies down to the essential issue, that would be helpful for us and probably for you as you debug.

That being said, one of the most useful things I have found is to add st.write(st.session_state) at the beginning of the app, and at the end, and at various places in-between. That can be very helpful for tracking down when exactly things are changing.

Thank you blackary for your response!
So bascially Iā€™m creating a English speaking practice app and I have two tabs, one for the questions and the recorder and another tab for generating example answer using openai.
the following is the tab1

    with tab1:
        with col1:
            if st.button("ā†"):
                selected_index = (selected_index - 1) % len(selected_options[selected_topic])
        with col3:
            if st.button("ā†’"):
                selected_index = (selected_index + 1) % len(selected_options[selected_topic])
             mp3_filename = None
            audio = None
            # Add the audiorecorder component to the container
            audio = audiorecorder()      

            # This is for switching questions 
            if "prev_selected_topic" not in st.session_state:
                st.session_state.prev_selected_topic = selected_topic
            if "prev_selected_index" not in st.session_state:
                st.session_state.prev_selected_index = selected_index

            # when question is switched to the next one, clear the audio and mp3_file
            if st.session_state.prev_selected_topic != selected_topic:
                audio = None
                mp3_filename = None
                st.session_state.prev_selected_topic = selected_topic

            if st.session_state.prev_selected_index != selected_index:
                audio = None
                mp3_filename = None
                st.session_state.prev_selected_index = selected_index

           // after recording, the audio will be processed and shown
            if audio is not None and len(audio) > 0:
                mp3_filename = process_audio(audio)

            if mp3_filename is not None and os.path.exists(mp3_filename):  # Add the os.path.exists check
                with open(mp3_filename, 'rb') as f:
                    mp3_audio = f.read()
                st.audio(mp3_audio, format="audio/wav")

And here are part of the script for tab2:

                with st.expander("Vocabulary", expanded=True):
                    vocab_placeholder = st.empty()
                    if st.button("Generating vocabulary"):
                        with st.spinner("Generating"):
                            new_token = ""
                            response_message = ""
                            inspiration = get_vocab(combined_text)
                            output_placeholder = st.empty()  
                            for chunk in inspiration:
                                if "choices" in chunk:
                                    for choice in chunk["choices"]:
                                        ....

the problem is, when I switch to the next question, the audio player will be gone, as shown in tab1, however, when I go to the tab2 for some examples, after clicking the button and going back to the tab1, I will find that the audio player start showing again! :joy:

Iā€™ve also tried st.write(st.session_state), and turned audio and mp3_filename variables to st.session_state, but it doesnā€™t solve the problem :hot_face:

Thanks for giving more details ā€“ that is helpful. But, I still donā€™t have enough code to actually be able to test it out and see what the issue is that you are facing. Could you please simplify your code into a single, complete script that reproduces the issue? See this post from StackOverflow about creating a minimal, reproducible example How to create a Minimal, Reproducible Example - Help Center - Stack Overflow

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