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.