A lot of you have asked over the years about the best way to have a login page that redirects you to another page, and changes which pages are available in the sidebar based on whether someone is logged in or not. I’ve also given a number of different answers to that question on here, and even added new features to my st_pages library to make this easier. But, now I have a new solution which is my favorite, and uses some new recent streamlit features:
st.switch_page and the new hideSidebarNavigation config from streamlit 1.30
Combining these together, I made an example app which:
Presents a log-in screen with no pages visible in the sidebar at first
After you log in, it navigates to a different page, and the login page is not visible in the sidebar
If you log out, it redirects you back to the login page, and again shows no other pages in the sidebar
If you try to visit one of the other pages while logged out (by going to the url), you will be redirected back to the login page without seeing any content (to be honest, this part is still a little hacky, but it works!)
Hey, I just saw this post, and it’s super helpful!
I am using this approach to show my menu items on the sidebar, but it’s ALSO showing me the actual pages in the /pages directory, as well as the one I’ve added using this method.
One more question, though… is there any way to move the menu items up higher on the sidebar?
I’m going to guess not, as it looks like there’s no option to render the menu higher up since that’s where the built-in multipage navigation usually sits.
Please note that you’ll need to apply this on all pages in your multi-page app, which may be easier if you create a function for this CSS styling then import this function in all the pages.
Thanks @dataprofessor and @blackary , I appreciate the responses! I will just live with the menu items where they are- I have had mixed results with the CSS injection.
Yeah, I overwrote the “built-in multipage navigation” quite easily.
In my navigation.py, make_sidebar() function, CSS editing of h1 for padding-top, stAppViewblockContainer, stSidebar, stSidebarUserContent for heights/widths/paddings etc. for everything in the sidebar including app icons. You can make your app much cleaner relatively easily.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.