New login page navigation example with streamlit 1.31

Hey Fellow Streamlitters,

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:

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!)

The app is here: https://app-app.streamlit.app/
The code is here: GitHub - blackary/streamlit-login

The bulk of the code for handling this is in navigation.py, and is used by adding these line to each page in the app.

from navigation import make_sidebar

make_sidebar()

Take a look and see what you think, and let me know if you have any suggested improvements!

4 Likes

I tried this one and still got errors.
But actually I always get errors from st.switch_page in my pc. I’m using windows.

This is my local directory. And I didn’t touch anything from your repo.

Is this browser’s error?

Ah, sorry about that – it’s a newly discovered bug on Windows StreamlitAPIException: Could not find page: 'pages/name.py'. Must be the file path relative to the main script, from the directory: .. · Issue #8070 · streamlit/streamlit · GitHub, and we’re working on a release to fix it

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.

I have configured the config.toml with

[client]
showSidebarNavigation = false

Is there anything else I should do or check?

Hah… nevermind! My .streamlit folder was not in the correct location.

Thanks for your work!

1 Like

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.

No, there’s not a way to move it – you could surely hack it via CSS, but then it would jump on page switch.

Hi @DadOfDubs

If styling via CSS as @blackary mentioned, there’s actually an FAQ article on this: FAQ: How to customize the style or appearance of your Streamlit app - #2

Perhaps you could try adjusting the margin-top property for the stSidebarNav element:

[data-testid="stSidebarNav"] {
   margin-top: 80px;
}

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.

Hope this helps!

2 Likes

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.

2 Likes

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.