Page refreshing after pressing a button

I am trying to build a UI for a scrapping script i have using streamlit, I am using streamlit version 1.39.0. the issue i am facing and help with is when i press a button, specifically the button “submit” in line “submitted = st.form_submit_button(“Submit”)”, which line 106 in the code below. the page resets to the previous page i have, i have tried multiple things, such as session states and forms to solve this issue, with no luck so if anyone could help I’d be super appreciative. here is the code:
import streamlit as st
from users import user_data # Assuming user_data is a dictionary of usernames and passwords.
from delete_DFs import delete_csv_and_xlsx_files
from main2 import start_scraping_task, get_task_progress, task_progress # Import task_progress dictionary
import time

Function to check user credentials

def check_credentials(username, password):
return username in user_data and user_data[username] == password

Streamlit app

def main():
# Clean up old files on app start
delete_csv_and_xlsx_files()

# Page 1: User Authentication
if "authenticated" not in st.session_state:
    st.session_state["authenticated"] = False

if not st.session_state["authenticated"]:
    st.title("User Authentication")

    # Store username and password in session state to retain after login
    if "username" not in st.session_state:
        st.session_state["username"] = ""

    if "password" not in st.session_state:
        st.session_state["password"] = ""

    # Input fields for authentication
    st.session_state["username"] = st.text_input("Username", value=st.session_state["username"])
    st.session_state["password"] = st.text_input("Password", type="password", value=st.session_state["password"])

    if st.button("Login"):
        if check_credentials(st.session_state["username"], st.session_state["password"]):
            st.success("Login successful!")
            st.session_state["authenticated"] = True
            st.session_state["email"] = ""  # Reset email for new session
        else:
            st.error("Invalid username or password.")
else:
    show_email_input_page()  # Show email input page once logged in 

def show_email_input_page():
st.title(“Email Input Page”)

if "email" not in st.session_state:
    st.session_state["email"] = ""

# Input email
st.session_state["email"] = st.text_input("Enter your email (e.g., abc.def@devoteam.com)", value=st.session_state["email"])
email = st.session_state['email']

# Button to check for ongoing task
if st.button("Check for Ongoing Task"):
    # Check if the email is in task_progress dictionary
    if st.session_state["email"] in task_progress:
        display_progress_page(email)  # Show progress if there is an ongoing task
    else:
        # Add the email to the task_progress dictionary with a default progress value
        task_progress[st.session_state["email"]] = 0  # Default progress
        st.success("No ongoing tasks found. You can proceed.")
        st.session_state['can_proceed'] = True  # Allow proceeding

# Check if user can proceed and show the button
if st.session_state.get('can_proceed', False):
    if st.button("Proceed to Search Input"):
        show_input_page(email)  # Pass email to the next page

def show_input_page(email):
st.title(“Search Input Page”)

# Initialize a flag in session state to track submission
if "submitted" not in st.session_state:
    st.session_state["submitted"] = False

with st.form("input_form"): 
    # Input fields
    if "keywords" not in st.session_state:
        st.session_state["keywords"] = ""
    keywords = st.text_input("Enter keywords for search (comma-separated)", value=st.session_state["keywords"])

    # Ensure selected option persists in session state
    if "selected_option" not in st.session_state:
        st.session_state["selected_option"] = "الرجاء الاختيار"  

    # Dropdown menu with choices
    dropdown_options = ["الرجاء الاختيار", "التجارة", "المقاولات", "التشغيل والصيانة والنظافة للمنشآت",
                        "العقارات والأراضي", "الصناعة والتعدين والتدوير", "الغاز والمياه والطاقة",
                        "المناجم والبترول والمحاجر", "الإعلام والنشر والتوزيع", "الاتصالات وتقنية المعلومات",
                        "الزراعة والصيد", "الرعاية الصحية والنقاهة", "التعليم والتدريب",
                        "التوظيف والاستقدام", "الأمن والسلامة", "النقل والبريد والتخزين",
                        "المهن الاستشارية", "السياحة والمطاعم والفنادق وتنظيم المعارض",
                        "المالية والتمويل والتأمين", "الخدمات الأخرى"] 

    # Set dropdown value based on session state
    option = st.selectbox(
        "النشاط الاساسي", dropdown_options, 
        index=dropdown_options.index(st.session_state["selected_option"]) 
        if st.session_state["selected_option"] in dropdown_options 
        else dropdown_options.index("الرجاء الاختيار"))

    st.session_state["selected_option"] = option

    # Submit button within the form
    submitted = st.form_submit_button("Submit")
    if submitted:
        st.session_state["submitted"] = True  # Mark the form as submitted
        # Update session state only on submission
        st.session_state["keywords"] = keywords 
        # (No need to update st.session_state["selected_option"] here)

# Check the submission flag outside the form
if st.session_state["submitted"]:
    search_keywords = [kw.strip() for kw in st.session_state['keywords'].split(",")]
    st.session_state.sync()  
    start_scraping_task(email, search_keywords, st.session_state["selected_option"])
    st.success(f"Scraping started. Results will be emailed to: {email}")
    st.session_state["submitted"] = False  # Reset the flag after processing

def display_progress_page(email):
st.title(“Scraping Progress”)

# Progress display
progress_bar = st.progress(0)
progress_placeholder = st.empty()  # Placeholder for the progress message

while True:
    progress = get_task_progress(email)  # Use the email from the parameter
    if progress >= 100:
        progress_placeholder.success("Scraping completed!")
        del task_progress[email]  # Remove user email from progress dictionary
        break
    elif progress == -1:
        progress_placeholder.error("An error occurred during scraping.")
        del task_progress[email]  # Remove user email from progress dictionary
        break
    else:
        progress_placeholder.info(f"Scraping in progress: {progress}% completed.")
        progress_bar.progress(progress)
        time.sleep(2)  # Sleep for a short while before checking the progress again

if name == “main”:
main()

Hi @omarkamalabuassaf1, welcome to the forum!

Can you please edit your post to make your entire code snippet in a code block? You can do this by putting ``` above and below your code.

It’s also a great idea to simplify down your code to the smallest possible snippet which still shows the issue. This will help you in your own debugging, and will make it more likely for others on the forum to be able to spot the issue.

As a general rule-of-thumb for debugging things like this, I find that adding st.sidebar.write(st.session_state) or something similar at the top and bottom of my code (and in the middle sometimes) is often really helpful for issues like this, as it helps me see what’s going on with session state, and see if the values are what I expect them to be.