how to implement equivalent of a for loop in streamlit ?

Context: i have a python script that does analytics based on a csv file entered by user. Detailed flow:

  1. script asks user to upload a csv file.
  2. sets up a connection with sqlite database
  3. goes through each row in csv and tries to find match in database. if details exist in database, it goes ahead. if not, it pauses script execution and asks user for input. this logic is implemented in a for loop
  4. after going through each row of csv, it does some analytics and presents output to user

i am currently making use of turtle package’s screen.textinput() to talk to user using a dialog.

what I need help with:
i find streamlit more capable and user-friendly than turtle package, and interested to replace everything with streamlit. How can I -

  1. implement an equivalent of for loop in streamlit?
  2. control when my app reruns?
  3. pause my app’s execution flow? and make a user input mandatory before script proceeds ahead

appreciate any response. thank you in advance!

Basically, Streamlit already has a built-in loop. Each run of your script needs to handle one iteration of your loop (or multiple runs need to handle one iteration if the user needs to do more than one thing in a loop). Use session state to track where you are in handling your loop.

import streamlit as st

if "data" not in st.session_state:
    st.session_state.data = [1,2,3,4]
    st.session_state.run_loop = False
    st.session_state.run_count = 0

def start_loop():
    st.session_state.run_loop = True

def stop_loop():
    st.session_state.run_loop = False

if not st.session_state.run_loop:
    st.button("Start loop", on_click=start_loop)
    st.stop() # Prevent the "else" part from being indented below

# Else (st.session_state.run_loop is True)
if st.session_state.run_count >= len(st.session_state.data):
    # Stop and reset the loop when you get to the end of the data
    st.session_state.run_loop = False
    st.session_state.run_count = 0
    st.rerun()

st.write(f"You are on run count {st.session_state.run_count}")
if st.button("Next"):
    # Process your current line then increment to the next one
    st.session_state.run_count += 1
    st.rerun()