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!
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
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
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.