Navigate Multipage App with Buttons instead of Sidebar

@blackary Since this will allow opening another page using buttons, is there an option to remove the sidebar pages option as it won’t make any sense

1 Like

Hey, I am using your approach for my app. Everything works perfectly in my local. However, when I deploy the app onto AWS, the switch_page() gives an error, which says that.

ValueError: Could not find page <name_ofmy_page>. Must be one of ['init ']

It sounds like your other pages aren’t actually being loaded – do you see them in the sidebar?

Can confirm that this method doesn’t work on st 1.26.0. I keep getting errors like this:

ImportError: cannot import name '_RerunData' from 'streamlit' (/Users/behnam/opt/anaconda3/envs/streamlit-app/lib/python3.11/site-packages/streamlit/__init__.py)

or

ModuleNotFoundError: No module named 'streamlit_extras.<my-app-name>'

the first error occurs if I don’t use the st_extras package and simply use the switch_page function defined above. The second one occurs when I use the st_extras package, import extra and modify the switch_page function with my own app name.

@behnamoh Did you try using it like this?

from streamlit_extras.switch_page_button import switch_page

want_to_contribute = st.button("I want to contribute!")
if want_to_contribute:
    switch_page("Contribute")

Also make sure switch_page argument is single quotes

switch_page('Trial')

Exactly how @mathcatsand and @behnamoh did in their illustrated above. This is in contrast to show_pages which uses double quotes. This one hung me up for a good hour.

That is not correct, actually. Python treats single quotes and double quotes the same, so either one will work fine.

Thank you for setting the record straight. I must have changed something else in the same commit. I don’t have time to figure out right now. Fortunately it all works.

can i build 2 pages?. the one is excel input page(that contains welcoming to the user) and then after user input the excel. page will be redirect to main page that will show analytic from user input

@blackary I keep getting an intermittent st.rerun() is a no-op error.

What have I implemented

  1. I use switch_page() function to successfully switch between main <> sub page
  2. This is called from 2 different ways - 1 as a callback on button click and 1 when a different button is clicked but no call-back method is used.

My guess is the issue occurs when the callback calls switch_page()

The page however ends up switching and there is this error that just gets displayed.

Is there a way to then just suppress this error from coming to the UI since this feels like a limitation of switch_page() being used in a callback ?

@streamlit_is_fun Good question – yes, I think this is simply a limitation of switch_page, and the fact that it raises a RerunException to trigger the page switch. Because of that, I think it simply won’t work in a callback for now without that error.

It might be possible to change the behavior slightly, but for now I would recommend just not using it in a callback.

@blackary - Any way of suppressing the error from coming up on the UI though ?
For now the setup is such that I cannot change the place from where switch_pages is called, but I rather suppress the error from being shown to the user

You can accomplish this by replacing st.warning with a no-op, like this:

import streamlit as st


def no_op(*args, **kwargs):
    pass


st.warning = no_op


def callback():
    st.rerun()


st.button("Rerun", on_click=callback)

Hey everyone!

Wanted to share the latest updates on this with Release 1.30.0. You can now natively program page switching using st.switch_page() as well as disable the sidebar navigation. :balloon:

More info in the docs. Looking forward to hearing what you think!

2 Likes