Avoiding rerun when using switch_page with button

I am working on a multipage app where i need to switch pages on click of a button. I have used switch_page to accomplish this and it does work. But I noticed the entire page reruns before switching to the desired page upon button click, which is an issue since the current page takes significant time to process due to LLM calls. I have replicated the basic structure of the main code where I need to switch pages, below i notice the processing spinner runs again upon button click before switching page, which is the main issue i am facing. How should I modify this to avoid rerun?

streamlit version: 1.38.0
python version: 3.11.5

main:

import streamlit as st

p1 = st.Page("views/page1.py")
p2 = st.Page("views/page2.py")
p3 = st.Page("views/page3.py")
nav_pages = [p1,p2,p3]

pages = {'p1':p1,'p2':p2,'p3':p3}

st.session_state['pages'] = pages 

pg = st.navigation(nav_pages)

pg.run()

page1:

import streamlit as st
import time

st.header("This is page 1")

def get_input():
    name = st.text_input("Enter your name")
    age = st.number_input("Enter your age in years", min_value=0)
    
    if name and age:
        with st.spinner("Processing..."):
            info = {'name': name, 'age': age}
            st.session_state['info'] = info
            time.sleep(2)  
    return name, age

def display():
    proceed = st.button("Proceed")
    if proceed:
        st.session_state['input'] = True

    if st.session_state.get('input', False) and 'info' in st.session_state:
        
        if 'pages' in st.session_state:
            page = st.session_state['pages'].get('p2', None)
            if page:
                st.switch_page(page)

if 'info' not in st.session_state:
    st.session_state['info'] = {}
if 'input' not in st.session_state:
    st.session_state['input'] = False

name, age = get_input()

if name and age:
    display()

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.