Loop while streamlit check box is true (checked)

Here is my code

import streamlit as ST
from datetime import datetime as DT
from multiprocessing import Process
import os
import time
import signal

def main():
ST.subheader(“My Process”)
while ST.checkbox(“Start_Button”)==True:
ST.write(“process is running”)

if name == ‘main’:
main()

When I run it using the normal streamlit run MyApp.py I get this error " There are multiple identical st.2601830728040438005 widgets with the same generated key." on the while checkbox line. Is there way to construct a loop that runs while a streamlit checkbox is ticked? Has anyone done something similar?

Replacing the while loop with an if condition should help.

Note that, everytime you interact with a widget on the Streamlit UI, your script re-executes from line 1.

If you really want a while loop there, it would be better if you can explain us your use-case so that we can propose workarounds.

Thanks for getting back to me on this.

As I explained in the previous email, I want a piece of code to keep looping while the checkbox is ticked. I am basically writing an app that pulls exchange rates on a real time basis every minute. If I replace the while with an “if” statement, the code executes once and stops. I want it to repeat over and over again while the checkbox in my example is ticked. The ST.write(“process is running”) should be replace with a sub routine called My_Process() that runs the code over and over again.

def My_Process():
.
.
.

while ST.checkbox(“Start_Button”)==True:
My_Process()

The following workaround should help:

if st.checkbox(“Start_Button”):
    while True:
        My_Process()

Because, as I said above, if you disable the checkbox, your python script will run again from the beginning, and it will not go into the if branch.

Please try it out first.

Thanks I think this work around should do the trick…

I tried that. Streamlit gets stuck in the while loop, unfortunately. It means once you enter the loop, you cannot get out of it - not even using a threading.