I’ve started to learn about Streamlit a few weeks ago, and I’m currently trying to create a supervisory application with it.
This is my current page structure:
As it is possible to see in the image, my main page is used as a login page.
What I’m trying to do now is to create a way for the user to go back to the login page (or even redirect the user to the login page for security reasons) using the switch_page function.
A very simple example is the one shown below: a page (into the /pages directory) containing only a button that is supposed to redirect the user to the main page (Login.py).
import streamlit as st
if st.button(':leftwards_arrow_with_hook:',key="1"):
st.switch_page("Login.py")
However, when I try to do this, I get the following error:
StreamlitAPIException : Could not find page: Login.py . Must be the file path relative to the main script, from the directory: pages . Only the main app file and files in the pages/ directory are supported.
What can I do to make this possible? I’ve tried different arguments in the st.switch_page function, such as “Web/Login.py”, “C:/Users/ … /Login.py”, etc. but nothing helped.
First, can you verify the version of Streamlit you are on, and (if possible) upgrade to the latest version? There was an issue a few versions back with how st.switch_page handled paths, but I thought it was resolved. I just want to make sure you’re not on that particular version before diving too deeply.
When you type streamlit run ... do you capitalize your filename in the command?
I was able to reproduce the issue on a Mac where the filename was capitalized but I used lowercase letters in the streamlit run ... command. The command line tool doesn’t seem to be case sensitive but it is once you get inside the Python environment. I’ll keep testing a bit and see if it’s the same on WIndows and such…
I’ve confirmed the problem occurs on macOS but not on Windows. I think we can consider this a bug, but fortunately, the workaround seems to be quite easy: make sure you follow the capitalization of your filename when you type out your streamlit run ... command. Let me know if that works for you. I’ll report the issue to the team.
I’m in a computer with Windows 11 Pro 23H2 installed, and my application is running in Chrome v124.0.6367.119 browser.
I tested the solution you provided, using only lowercase letters, but no changes. Even tried to create a simpler project with only 2 pages (both with very simple lowercased names) to check if that the problem is in the file path, and the problem continues. This is really weird… I’ll try to run this in another Windows computer and see how it goes. If I get any results I’ll post it here!
I really appreciate all your help and support!! Thank you! (:
Thanks for confirming this is occurring on a Windows machine and that it’s slightly different than what I can reproduce on a Mac. I’ll keep looking into it.
If you can add info to the GitHub issue I opened, that’ll be helpful to the developers to give them info to reproduce the problem in all the ways it can trigger.
@Storz You might try making everything lowercase to eliminate that piece for certain as a test (even though I couldn’t trigger it Windows, so maybe that’s not it). Also, try switching to other pages to confirm things are operating from the correct relative location (which I believe Ferdy’s line of questioning is hinting at).
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.