Access page from the url

I have a streamlit app that uses st.navigation , here per of my app.py

home_page = st.Page("pages/Home.py", title="Quanta Home", icon=":material/add_circle:")
login_page = st.Page("pages/Login.py", title="", icon=":material/add_circle:")
doc_page = st.Page("pages/Documents.py", title="Documents", icon=":material/add_circle:")
chat_page = st.Page("pages/Chatbot.py", title="Chatbot", icon=":material/add_circle:")
register_page = st.Page("pages/Register.py", title="Register", icon=":material/add_circle:")
profile_page = st.Page("pages/Profile.py", title="Profile", icon=":material/add_circle:")
usage_page = st.Page("pages/Usage.py", title="Usage", icon=":material/add_circle:")

get_session_up()

#if user logged in show a Logout button on sidebar
if st.session_state["logged_in"]!=None:
    logout = st.sidebar.button("Logout")
    if logout:
        logout_user()


if st.session_state["logged_in"]!=None:
    pg = st.navigation([home_page, doc_page, chat_page, profile_page, usage_page], position="sidebar")
else:
    pg = st.navigation([login_page, register_page], position="hidden")

pg.run()

When I try to access a page like Profile via https://localhost:8501/Profile I got this

The page that you have requested does not seem to exist. Running the app’s main page.

Any idea what am I doing wrong?

More in general I am looking for a way to access https://localhost:8501/documents or any other URL

@Fausto

You can use st.switch_page

if st.session_state["logged_in"] != None:
    st.switch_page("pages/Home.py")
else:
    st.switch_page("pages/Login.py")

thanks @Rushmore that I can do :slight_smile:
What I am trying to achieve is being able to type https://localhost:8501/Login in the browser and land on the page
What I got is this:

@Fausto Sorry my mistake, did not read that well enough.

Your issue is here:

if st.session_state["logged_in"]!=None:
    pg = st.navigation([home_page, doc_page, chat_page, profile_page, usage_page], position="sidebar")
else:
    pg = st.navigation([login_page, register_page], position="hidden")

If the user is logged in, the login_page and register_page is not registered as valid pages as you can see from your code. So you cannot navigate to it. You can only navigate to it when st.session_state["logged_in"] == None, the opposite of your initial/first condition.

You are right on this one, but I got the same issue even if try to navigate https://localhost:8501/Documents

Please clarify, you are using streamlit locally and are navigating to http and not https?

Secondly, I just tried it and it works depending on st.session_state["logged_in"].
Check what value the state is for st.session_state["logged_in"]. st.write it or print it to view it in the terminal. Perhaps st.session_state["logged_in"] is None?

Lemmi clarify more. This is my menu:


I can navigate , for example, Documents link by clicking on Documents in the menù.
And I see this in the URL bar:
Screenshot 2025-03-12 alle 15.29.51

BUT

if I type localhost:8501/Documents and I navigate to it I got the error

Okay. Can I assume the below is the logic to get the current user loggedin status?

correct its the method that I use to bring up the cookies in case they exist

Could I get a glance of what it looks like? You can anonymise name of the cookie.

def get_session_up():
    # Load session from cookies
    if "cookies" in st.session_state and st.session_state["logged_in"] != True:
        cookies = st.session_state["cookies"]
        #I need to check if cookies contatins the logged_in key
        if cookies is None:
            return False
        if "logged_in" not in cookies:
            return False
        st.session_state["logged_in"] = cookies['logged_in']
        st.session_state["user_id"] = cookies['user_id']
        st.session_state["user_email"] = cookies['user_email']

        # load the google calendar credentials from the token.pickle file
        if "credentials" not in st.session_state:
            st.session_state["credentials"] = None
        if st.session_state["credentials"] is None:
            token_path = os.path.join("tokens_folder", f"token_{st.session_state['user_id']}.pickle")
            if os.path.exists(token_path):
                with open(token_path, "rb") as token:
                    creds = pickle.load(token)
                    st.session_state["credentials"] = creds
            else:
                st.session_state["credentials"] = None

        return True
    else:
        return False

I use this to grab the cookies after a refresh when the session is cleared.

Thanks, so I was able to somewhat mimic conditions of the app below. Correct me if I missed anything.

Firstly, Rename folder/directory that have your app pages. From pages to app_pages might suffice.
Getting an error on the console that says: “st.navigation was called in an app with a pages/ directory. This may cause unusual app behavior. You may want to rename the pages/ directory.”

Secondly, the below is a simulation of what I think happens in your app.

USER INITIALLY LOGGED IN

def user_logged_in_already():

    if "logged_in" not in st.session_state:
        st.session_state["logged_in"] = None 

    if "cookies" not in st.session_state:
        st.session_state["cookies"] = {
            "logged_in":True,
            "user_id":"user_id_here",
            "user_email":"email@gmail.com"
        } 

    if "user_id" not in st.session_state:
        st.session_state["user_id"] = ""

    if "user_email" not in st.session_state:
        st.session_state["user_email"] = ""

    def get_session_up():
        # Load session from cookies
        if "cookies" in st.session_state and st.session_state["logged_in"] != True:
            
            cookies = st.session_state["cookies"]
            #I need to check if cookies contatins the logged_in key
            if cookies is None:
                return False
            if "logged_in" not in cookies:
                return False
            st.session_state["logged_in"] = cookies['logged_in']
            st.session_state["user_id"] = cookies['user_id']
            st.session_state["user_email"] = cookies['user_email']

            return True
        else:
            return False
        
    get_session_up()

    home_page = st.Page("pages/Home.py", title="Quanta Home", icon=":material/add_circle:")
    login_page = st.Page("pages/Login.py", title="", icon=":material/add_circle:")
    doc_page = st.Page("pages/Documents.py", title="Documents", icon=":material/add_circle:")
    chat_page = st.Page("pages/Chatbot.py", title="Chatbot", icon=":material/add_circle:")
    register_page = st.Page("pages/Register.py", title="Register", icon=":material/add_circle:")
    profile_page = st.Page("pages/Profile.py", title="Profile", icon=":material/add_circle:")
    usage_page = st.Page("pages/Usage.py", title="Usage", icon=":material/add_circle:")


    #if user logged in show a Logout button on sidebar
    if st.session_state["logged_in"]!=None:
        logout = st.sidebar.button("Logout")
        if logout:
            st.write("logout")


    if st.session_state["logged_in"] != None:
        pg = st.navigation([home_page, doc_page, chat_page, profile_page, usage_page], position="sidebar")
    else:
        pg = st.navigation([login_page, register_page], position="hidden")

    pg.run()

get_session_up()

USER NOT INITIALLY LOGGED IN

def user_not_logged_in():

    if "logged_in" not in st.session_state:
        st.session_state["logged_in"] = None 

    if "cookies" not in st.session_state:
        st.session_state["cookies"] = None 

    if "user_id" not in st.session_state:
        st.session_state["user_id"] = ""

    if "user_email" not in st.session_state:
        st.session_state["user_email"] = ""

    def get_session_up():
        # Load session from cookies
        if "cookies" in st.session_state and st.session_state["logged_in"] != True:
            
            cookies = st.session_state["cookies"]
            #I need to check if cookies contatins the logged_in key
            if cookies is None:
                return False
            if "logged_in" not in cookies:
                return False
            st.session_state["logged_in"] = cookies['logged_in']
            st.session_state["user_id"] = cookies['user_id']
            st.session_state["user_email"] = cookies['user_email']

            return True
        else:
            return False
        
    get_session_up()

    home_page = st.Page("pages/Home.py", title="Quanta Home", icon=":material/add_circle:")
    login_page = st.Page("pages/Login.py", title="", icon=":material/add_circle:")
    doc_page = st.Page("pages/Documents.py", title="Documents", icon=":material/add_circle:")
    chat_page = st.Page("pages/Chatbot.py", title="Chatbot", icon=":material/add_circle:")
    register_page = st.Page("pages/Register.py", title="Register", icon=":material/add_circle:")
    profile_page = st.Page("pages/Profile.py", title="Profile", icon=":material/add_circle:")
    usage_page = st.Page("pages/Usage.py", title="Usage", icon=":material/add_circle:")


    #if user logged in show a Logout button on sidebar
    if st.session_state["logged_in"]!=None:
        logout = st.sidebar.button("Logout")
        if logout:
            st.write("logout")


    if st.session_state["logged_in"] != None:
        pg = st.navigation([home_page, doc_page, chat_page, profile_page, usage_page], position="sidebar")
    else:
        pg = st.navigation([login_page, register_page], position="hidden")

    pg.run()

user_not_logged_in()

In the latter - user_not_logged_in - navigating to Documents brought up the error. In the former it did not.

Maybe have a look at how you are calling the cookies.

Feel free to correct me if anything is wrong.

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