streamlit

streamlit: how to navigate to a page directly after file_uploader?


My streamlit app has two pages: a home page which shows some info and allows the user to load their data with st.file_uploader. Once the user uploaded their data, I would like to navigate automatically to the second page which does the processing.

Currently I show a link that the user has to click, like this:

uploaded_file = st.file_uploader("Choose a file", type=['xlsx'])

if uploaded_file is not None:
    st.page_link(page='pages/1_Process.py', label='Start the processing')

I tried using on_change and switch_page but it does not work, as this causes the error 'Calling st.rerun() within a callback is a no-op'.

Is there any way to achieve this?


Solution

  • You could switch_page this way (without handling the on_change) :

    import streamlit as st
    
    uploaded_file = st.file_uploader("Choose a file", type=["xlsx"])
    
    if uploaded_file is not None:
        st.page_link(page='pages/1_Process.py', label='Start the processing')
        st.switch_page("pages/1_Process.py")
    

    enter image description here