I need to show progress bar per tabs independently in my Streamlit app.
I tried a demo code which updates the first tab’s progress bar, but it’s not working.
import streamlit as st
import time
def download_all_images():
for i in range(100):
print(f"value {i}")
st.session_state.progress1 = i
time.sleep(1)
def main():
st.title("Image Viewer with Progress Bars")
st.session_state.progress1,st.session_state.progress2,st.session_state.progress3 = 1,1,1
if st.button("Generate"):
download_all_images()
# Create tabs
tab1, tab2, tab3 = st.tabs(["Image 1", "Image 2", "Image 3"])
# Display content in each tab
with tab1:
st.progress(st.session_state.progress1)
with tab2:
st.progress(st.session_state.progress2)
with tab3:
st.progress(st.session_state.progress3)
if __name__ == "__main__":
main()
After clicking generate button, it looks like this:
with tab1:
bar = st.progress(0) # <-- Bar starts at zero
if st.button("Press to load"):
for i in range(101):
bar.progress(i) # <-- Updates bar
Having a single button to trigger all progress bars will not make them run at the same time. In your example, the bar in tab2 won’t start until the bar in tab1 is finished.
Your code works @edsaac . But I need to update it from external function.
My goal is, I am creating an Image Generation app that supports multi models and each model has a separate tab for it’s respective image.
The models are executed sequentially, so when a model image , say SDXL model is being generated , the SDXL tab need to show a progress bar, meanwhile the completed tabs need to show the image.
You could pass the DeltaGenerator object that st.progress returns to the function in charge of updating it, something like:
import streamlit as st
import time
def move_progress_bar(bar):
for i in range(1, 101):
bar.progress(i, text=f"Step {i}/100")
time.sleep(0.1)
st.write("Done!")
def main():
st.title("Image Viewer with Progress Bars")
tab1, tab2, tab3 = st.tabs(["Image 1", "Image 2", "Image 3"])
with tab1:
button_1 = st.button("Run bar 1")
bar_in_tab_1 = st.progress(0, text="Bar in tab 1")
if button_1:
move_progress_bar(bar_in_tab_1)
with tab2:
button_2 = st.button("Run bar 2")
bar_in_tab_2 = st.progress(0, text="Bar in tab 2")
if button_2:
move_progress_bar(bar_in_tab_2)
with tab3:
...
if __name__ == "__main__":
main()
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.