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.