How to redirect to another page after login or don't show the list of pages on the sidebar?

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:

1 Like

Are you on a windows machine? If so, there’s an open issue here 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 a suggested workaround 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 – can you take a look at that and see if it resolves the issue for you?

1 Like

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.

1 Like

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.

From the docs:

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.

2 Likes

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.

1 Like

Good suggestion. I’ve passed your comment to the team for st.switch_page. :+1:

2 Likes

Seeing how easy it will be to improve the error text!

We’re also working on removing the restriction on only files in the pages/ folder and allowing multi-page apps to use other files :slight_smile:

3 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.