hi guys im trying to redirect the login page to the home page after a successful login, but im having trouble figuring it out. i’ve reviewed the streamlit documentation on multipage apps but it doesn’t provide the solution im looking for. rn im using “st.switch_page(“pages/home.py”)” after a succesful login but this makes the home page also appear on the sidebar before even logging in. idk if theres a way to hide it in the sidebar or another solution?? ty in advance
Could you post a sample minimal code that we can test?
You might be interested in this post, which shows how you can hide pages until logging in using the st_pages package.
The one piece that is different in your request is that you want to hide the home page after you log in, which you can do by switching hide_pages([])
to hide_pages(["login"])
(or whatever your log in page is called that you want to hide).
Note that this hiding simply happens by CSS, and needs to be done on every page of your app to make it work. For example, you might put this in every page in your app:
(The sleep isn’t necessary, just makes it more obvious what’s happening before the page switch)
import streamlit as st
from time import sleep
from st_pages import hide_pages
hide_pages(["login"])
if st.button("Log out"):
st.session_state["logged_in"] = False
st.success("Logged out!")
sleep(0.5)
st.switch_page("login.py")
Note also that st.switch_page is part of the official streamlit package, and works slightly differently from hide_pages, which comes from the unofficial st_pages package.
Here’s a new improved version of this, which uses the new features of streamlit 1.31.0 GitHub - blackary/streamlit-login
Here’s an example app in action https://app-app.streamlit.app/
For me the code at streamlit 1.31.0 GitHub - blackary/streamlit-login does not work
When ‘run’ the server it already starts with all the pages listed in the sidebar
after login, it shows the labelled version below as
where Info = Landing Page
Backtest = Backtest results
and Liverun = Live run results
the code is more or less a copy paste of the code of github code
finally figured out
…the trick is config.toml entry which hides the sidebar
[client]
showSidebarNavigation = false
Hi @blackary,
I tested the app you shared and it has the functionality I want. My main issue is when I refresh the page at page1.py, it goes back to the login screen. How can I prevent this from happening?
Regards,
Andrew
When you refresh the page, you start a new session, and so st.session_state becomes empty. My app has code in it that checks to see if you’re logged in, and if not directs you to the login page.
There’s not a great way to keep someone logged even with a refresh unless you want to use cookies – you can find various posts about that on the forum, but there’s not an official method for writing cookies with Streamlit yet.
I tested your app. After login it goes to a page. But when I refresh the page, it again leads me to login page.
Correct: