New package st-pages: change page names and icons in sidebar without changing filenames

I have an authentication function with only one user credential and after I log in once, my streamlit application does not require logging in anymore as long as I don’t restart the whole application. This only happened after I started using st_pages. I would like the application to require log in every time there is a refresh on the page or closing the browser or restarting the application. Is there a solution to this problem?

Hi @Ralghs, I’m not sure how that would be related to st_pages, but could you post a small reproducible version of your code which demonstrates this issue?

import streamlit as st
from st_pages import Page, show_pages, add_page_title

def check_password():
    """Returns `True` if the user had a correct password."""

    def password_entered():
        """Checks whether a password entered by the user is correct."""
        if (
            st.session_state["username"] in st.secrets["passwords"]
            and st.session_state["password"]
            == st.secrets["passwords"][st.session_state["username"]]
        ):
            del st.session_state["password"]  # don't store username + password
            del st.session_state["username"]
            return True
        else:
            return False

    st.title("Log In")
    st.text_input("Username", key="username")
    st.text_input("Password", type="password", key="password")
    if st.button("Login"):
        if password_entered():
            st.session_state.password_correct = True
            st.experimental_rerun()
        else:
            st.error("😕 wrong username or password")
# Streamlit application
def main():
    if "password_correct" not in st.session_state:
        st.session_state.password_correct = False
    if not st.session_state["password_correct"]:
        check_password()
    else:
        add_page_title()
        show_pages(
            [
                Page("page1.py", "Home", "🥶"),
                Page("page2.py", "Page 2", ":books:"),
                Page("page3.py", "Page 3", ":books:"),
            ]
        )

if __name__ == "__main__":
    main()

If I use the multipage function recommended in the streamlit API library, I don’t have this problem but that does not allow me to have sections so I prefer st pages

Thanks so much for the script! That’s very helpful. Unfortunately, I wasn’t able to reproduce the issue you were seeing – I put in a username and password, and it hid the login form. But, when I refreshed the page, it required me to log in again.

I was able to kinda solve this problem by adding

if "password_correct" not in st.session_state or st.session_state.password_correct = False:
    check_password()
else:
    """code for this page"""

to every page but this raises a new problem:
After I log in once and call show_page(), the sidebar cannot be unshow which means the user can still click on the sidebar even thou they are not logged in. I had a look in the init.py file and did not find a method that can hind the sidebar. Is there a way around this? Maybe adding this code to every page is not the best solution.

I don’t currently have a solution for dynamically hiding pages from users (though there is a PR right now that I need to evaluate which might add this soon), so as of today you can’t dynamically show/hide pages in the sidebar based on the user status.

1 Like

This only works 1 percent of the time for me, I go the exact path to the page file, but always get the following:

Screenshot 2023-04-04 at 18.59.21

Can’t seem to get it right. or somewhere it deactivates.

1 Like

Hi @christian_heins, can you share an example repo where you’re seeing this behavior?

@Ralghs Thanks very much to mfriedy · GitHub, you can now dynamically hide pages in the sidebar in version 0.4.0 of st-pages!

Note that this is just a visual change, so it shouldn’t be relied on for anything sensitive/secure, but it works well for guiding the user towards what they should have access to.

2 Likes

Cool :slight_smile:

Recommendation: add a parameter in st_pages.Page named index (well u can name it another way), but this helps us put the pages in the order we want

1 Like

@Ivan_Schuster That’s a reasonable idea, but did you know that they show up in the order that you put them in the list?

1 Like

Didnt know that! Now it does seem to work yes :slight_smile: Thanks

1 Like

Here is the web app: https://christianheins-wggesucht-wggesucht-2lmx07.streamlit.app

Here is the repo: GitHub - christianheins/wggesucht

It seems to me that there is an issue with the initial st.pages setting for me: If I specify the pages as following

show_pages(
    [
        Page("Welcome.py", "Home"),
        Page("pages/2_Overview.py", "Overview"),
        Page("pages/3_Setup_Switching_Brand.py", "Setup: Switching for Brands"),
        Page("pages/4_Setup_Switching_Products.py", "Setup: Switching for Products"),
        Page("pages/5_Setup_Affinity_Product_Product.py", "Setup: Affinity for Product vs Product"),
        Page("pages/6_Setup_Affinity_Product_Brand.py", "Setup: Affinity for Product vs Brand"),
        Page("pages/7_Setup_Affinity_Product_L3.py", "Setup: Affinity for Product vs Category")
        ]
    )

It is unable to get the files in the pages folder and rename them correctly. I have one page outside of the pages, which is initiated with streamlit run.

1 Like

Can you share more details? Ideally, can you share the repo that you are having an issue with?

@blackary Thank you for the st_pages package,
while using it today the first time,
I encountered 2 issues, encoding issue is minor,
but don’t know how to resolve another one - pages on sidebar disappeared after clicking a radio button on your Example Four page

using: st-pages  Version: 0.4.4

Issue #63

PR #62 (fix encoding )

1 Like

Amazing package - makes life much easier!

Is it possible to make the URL follow the filename or make it custom instead of the name given in the Page class instance?

In your example app, I’d like URL to have “_” instead of “%”:
image

Thanks again!

1 Like

I think that is possible, but I haven’t gotten around to figuring out how to do that yet URL parsing · Issue #11 · blackary/st_pages · GitHub

Hi everyone,

I was wondering if there could be something related to st-pages that prevents Google SEO from recognizing the app?

This is my app:

This is what I get when I Google “site:phd-econometrics.streamlit.app” as suggested by docs.streamlit

Any help would be appreciated.

Thank you,
Justinas

1 Like

My best guess is that it just takes time to show up – how long as your app been up?