Every time I use a while loop, it seems Streamlit gets stuck inside it. Even if I tell the loop to break as soon as a certain condition is True, the condition remains the same. I also have tried putting the while loop inside a function and call it using a thread (foreground and background) and the result was the same.
Is there any workaround for this? Loops are one of the most used logics in programming languages.
Can you post a code snippet that demonstrates the behavior you’re describing? I’ve not of a situation where while loops don’t work, but happy to take a look.
Hi Randy, thank you for the reply. Your example worked just fine! By the way, I get stucked when I have a “while True” after a condition that checks if a checkbox is ticked. For example:
obj2 = st.sidebar.checkbox('On / Off')
if obj2:
while True:
print('Checkbox ON')
The example above was built thinking about the flow described by one user here in the forum, in which he states the code is always executed entirely from top to bottom in Streamlit, so that should be the way to go for the while loop.
But as I mentioned before, your example worked perfectly! Thank you.
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.