Skip to content

Instantly share code, notes, and snippets.

Last active March 25, 2024 18:44
Show Gist options
  • Save asehmi/0ad09842ec9df1b48e82e097163a9c88 to your computer and use it in GitHub Desktop.
Save asehmi/0ad09842ec9df1b48e82e097163a9c88 to your computer and use it in GitHub Desktop.
STqdm: A tqdm-like progress bar for Streamlit
# UPDATED: 5-MAY-2023
from multiprocessing import Pool, freeze_support
from time import sleep
import streamlit as st
# pip install stqdm
from stqdm import stqdm
c1, c2, _ = st.columns([1,1,1])
num_p = c1.number_input('Number of processes', value=5, min_value=1, max_value=100)
num_i = c2.number_input('Number of iterations', value=100, min_value=1, max_value=1000, step=100)
message = st.empty()
stqdm_container = st.container()
def sleep_and_return(i):
return i
def run_pool(n_processes=2, n_iterations=10):
with stqdm_container:
with Pool(processes=n_processes) as pool:
for i in stqdm(pool.imap(sleep_and_return, range(n_iterations)), total=n_iterations):
message.success(f'Iteration: {i}')
if __name__ == '__main__':
# On Windows an error was being thrown which suggested freeze_support() as a fix
# "An attempt has been made to start a new process before the
# current process has finished its bootstrapping phase...."
option ='Select an option', ['Run forever', 'Run once', 'Stop'], index=2, horizontal=True, key='options')
# Run forever (for demo purposes)
if option == 'Run forever':
while True:
run_pool(n_processes=num_p, n_iterations=num_i)
# More realistic use case to run once and stop
elif option == 'Run once':
run_pool(n_processes=num_p, n_iterations=num_i)'Waiting to run again...')
Copy link

asehmi commented Apr 19, 2022


Copy link

Thanks for this. A question, why do we need the while loop? On l. 35, and does it automatically breaks out if the workers are done?

Copy link

asehmi commented May 5, 2023

The while loop is not required. It's there only to have the demo running indefinitely. Once the worker pool has run, the program ends.

I'll update the gist to demonstrate this.

Copy link

Thanks you! I'll try this out in some projects of mine soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment