I have a login streamlit page and after a succesful login I do
if not check_password():
st.stop()
st.sidebar.success("Logged in!")
sleep(0.5)
st.switch_page("other_pages/app.py")
but then I get this error
streamlit.errors.StreamlitAPIException: Could not find page: 'other_pages/app.py'. Must be the file path relative to the main script, from the directory: ai-portal.
even though I have the path correct. I tried with renaming the folder pages and then it works but then I get a list of the pages in the sidebar and the user can freely move from one to the other without login in like this:
I’m in Linux. If I put the app.py file in the pages folder then it works fine and it finds the file with no problem. But if I change the name of the folder to, say, other_pages and then I use the same path but with other_pages like other_pages/app.py then it throws me the error.
I would have no problem using the pages/app.py path but the issue is that when I use that path, streamlit put the list of the pages on the sidebar to navigate to and the user can freely go from one page to the other so the login page becomes a moot point.
Only Python files structured in compliance with Streamlit’s multipage structure will work. (Your main file, and one in a pages/ folder relative to your main file.) So this is expected.
When st.switch_page is called, the current page execution stops and the specified page runs as if the user clicked on it in the sidebar navigation. The specified page must be recognized by Streamlit’s multipage architecture (your main Python file or a Python file in a pages/ folder). Arbitrary Python scripts cannot be passed to st.switch_page.
I can look at rearranging the text in the docs to call out the warning in the parameter description if that might be clearer…
To hide the default navigation, use the configuration option, showSidebarNavigation=false. This tutorial goes over an example.
Thank you so much! Hiding the sidebar navigation did the trick for me!
I think the error message in the case of using switch_page with code outside the pages folder could be more descriptive because right now it makes it seem that it’s a path formatting issue but it’s actually a path restriction issue.
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.