Hide/show pages in multipage app based on conditions

Hi all!

I know that streamlit’s new native support for multipage apps (documented here) works in a way such that each file in the pages folder will be a page, and thus be rendered in the application’s sidebar.

I am trying to make a streamlit application such that when the application is visited, only one page is shown in the sidebar, namely the “Sign In” page. Upon successful sign-in, the application should show the other pages in the sidebar and hide the Sign In page.

To do this, I would need to hide/show pages in the sidebar based on certain conditions (in my case, based on whether the user has successfully signed in). However, it looks like whatever is in the pages folder WILL be rendered as a page on the sidebar by default, and there’s no way to customise which pages get rendered when.

Basically: is there a way to achieve conditional rendering of pages on the sidebar, using Streamlit’s native support for multipage apps?

Thank you!

3 Likes

I just got started with this myself. I’m probably not the best person to answer the question, but I think you might be able to do it with st.session_state?

This is a good topic and I’m curious if Streamlit has any (examples of) integrations with any of the major account creation and authentication providers for self-hosted deployments.

1 Like

Hey @tommaso-moro!

Conditional rendering of pages is a feature request we had quite a lot after releasing multipage apps, indeed! Currently, this is not natively supported, unfortunately, but it’s on our radar for next iterations. What you can do, is having all pages appearing in the menu but have them display different things depending on being signed up or not. Something along those lines:

# pages/sensitive_page.py
if not logged_in():
    st.warning("You must log-in to see the content of this sensitive page! Head over to the log-in page.")
    st.stop()  # App won't run anything after this line

sensitive_stuffs()
6 Likes

And definitely, @hack-r, the logged_in() mocked function above would use st.session_state under the hood!

1 Like