Disable button during on_click function evalution

Hi,

I am developing an application, which autoplay an audio file when a button is pressed (using markdowns). There are multiple button and each one would play different sound upon clicked.

Currently, I am using time.sleep function to complete the execution of audio file before the code is executed again for other inputs. However, the user can press another button before finishing the audio, which is not optimal. Is there a way to disable the button as soon as it is pressed, and enable it after the audio is completed.

Thanks,

1 Like

Hi Aakashagr! Try this code. It must disable button when you press it, and enable it after the audio is completed but only if user can’t stop audio.

import streamlit as st
import time
import librosa


placeholder = st.empty()
btn = placeholder.button('Button', disabled=False, key='1')
audio_file = open('audio_example.oga', 'rb')
audio_bytes = audio_file.read()
st.audio(audio_bytes, format='audio/ogg')
if btn:
    placeholder.button('Button', disabled=True, key='2')
    duration = librosa.get_duration(filename='audio_example.oga')
    time.sleep(duration)
    placeholder.button('Button', disabled=False, key='3')
    placeholder.empty()
    st.experimental_rerun()
2 Likes

Thanks a lot for your solution. It did help me solve this problem using placeholder. However, I hope that generating the button thrice would not slow down the server (especially when there are ~20 buttons)

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