I've got the following code:
import streamlit as st
from time import sleep
import pandas as pd
import numpy as np
from stqdm import stqdm
def something_heavy(x):
sleep(1)
return x * 100
df = pd.DataFrame(np.random.randint(0, 100, size=(10, 2)), columns=list('AB'))
stqdm.pandas(desc='Doing smth heavy')
df['C'] = df.progress_apply(lambda row: something_heavy(row['A']), axis=1)
st.write(df)
I need to perform some heavy operation with the dataframe and make the app user see the progress.
The bar looks fine and shows the progress correctly. But if the app is somehow interrupted (for example, the browser tab is closed) before the progress_apply finishes, the app will get into an infinite loop on the next run. The only thing that helps in this case is rebooting the app (restaring the console).
I've tried using st.session_state and various other things but nothing works. How can I solve this?
As discussed on streamlit forum, this is related to a locking issue between streamlit and tqdm. I have not fully identified the reason yet, but it happens mainly on windows.
A workaround provided here is to change the lock, at the beginning of your script.