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

This feels better than changing the working directory at runtime.

2 Likes

Hi
I managed to get it worked.

However, I have another issue. When I use hide_pages, the start of the line of the page is somehow shifted down. Have you seen this before?


1 Like

Is there a way to move the sidebar to the right?
My current code is:

from st_pages import Page, show_pages
show_pages(
    [
        Page("home_view.py", "faz", "🏠"),
        Page("far_view.py", "foo", ":file_folder:"),
        Page("nice_view.py", "bar", ":pencil:"),
        Page("view.py",'sample')
    ]
)

This creates a sidebar to the left, i want the sidebar to the right, how can i achieve this?

1 Like

Hi @Will1, that’s because hide_pages uses css to hide the pages, which involves inserting a small block of html into the page. Unfortunately, I don’t think there’s a way to avoid this, but you could do hide_pages at the end of the app code.

This method doesn’t handle the indentation, but if the main goal is dynamically changing the pages, you could also consider this method New login page navigation example with streamlit 1.31

Hi!

I have a possible lead on the source of the bug that causes the deactivation of customizations on st_pages. Indeed, I am also in a case where it was working very well, and after updating my application with things that are strictly unrelated to page names/icons, etc., st_pages stopped working.

I was using customization in .streamlit/pages.toml, with:

from st_pages import show_pages_from_config
show_pages_from_config(.streamlit/pages.toml)

And it got activated/deactivated without any apparent reason, but still worked locally. However, using:

import os
path_toml = os.path.join(os.getcwd(),".streamlit","pages.toml")
st.write(path_toml)

on my main page, I got two different things :

  • locally :

C:\Users\myname\files\Web app\my_folder\my_repo.streamlit\pages.toml

  • cloud :

/mount/src/my_repo/.streamlit/pages.toml

It worked with:

show_pages_from_config(path_toml)

(and even locally despite the absence of ‘/’ before ‘.streamlit’)

I’m thinking that maybe st_page has trouble managing different paths/Can’t find the toml file?

I should mention that I’m using Windows in France and I have a “packages.txt” file in which I have:

locales
locales-all

With a setting in one of my files:

import locale
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')

I don’t know if this is related, do you have any similarities in your errors?"

What is the error you’re getting?

Also, I assume that this line

show_pages_from_config(.streamlit/pages.toml)

Is actually

show_pages_from_config(".streamlit/pages.toml")

I’m really surprised that os.path.join doesn’t work properly on windows, but you might try this:

from pathlib import Path

raw_path = Path() / ".streamlit" / "pages.toml"

st.write("Path contents", raw_path.read_text())

show_pages_from_config(str(raw_path))

Hi @blackary

I recently stumbled upon the same issue as Debayan.

I have created a streamlit app intented to be used by a lot of people, and I want to control which pages they see in the sidebar on an individual level, of course without updating the view for everybody whenever a user logs on.

Are there any changes that make this possible now?

Yes, you can definitely do that, with a combination of st.experimental_user, and the methods that are now available with 1.31 to dynamically change the sidebar – you can see an example here: New login page navigation example with streamlit 1.31

Perfect! That was exactly the solution we were looking for.

Is there any way to make this menu scrollable like the default navigation menu?

Sure! You can put it in a container with a specific height, and then it will be scrollable

Can we create pages with depth > 1?

@Sairam No, just depth of 1 currently

Thank you!

1 Like

@blackary - Is it also possible to automatically hide the section label if all the pages in that section are hidden - If the feaure is not available at the moment I can create an issue and try to pick it up

Hi @blackary ,

Can you pls help on the same issue where the indentation is not applied to the pages below the Section . Also the section is clickable as per the document its should not be.

Thanks in advance.

Pls find the below code as a reference:

from st_pages import Page, Section, add_page_title, show_pages,add_indentation

show_pages(
[
Section(name=“Section 1”),
Page(“HLK.py”, “Page 1”),

    Section(name="Section 2"),
    Page("AVL.py", "Page 2")
    
]

)
add_indentation()

Screenshot 2024-07-01 112728

Regards,
Madhusudan M

@blackary Eager to check out 1.0.0 – would also be helpful if we can pass a list of page hashes to hide instead of the page names as different sections could have same page names – Happy to pick this up as well once version 1.0.0 is released

Hello @blackary,

Hope you are doing well!
st-pages works perfectly fine in showing menus and icons when running the streamlit app locally.

But in a container, the method show_pages_from_config() and pages.toml dosen’t seem to work. I am using st-page version 0.5.0

Is there a solution to this?

Locally I am working on WindowsOS and the container is deployed to an AKS Cluster running on Ubuntu22.04

I referred to you previous comments regarding Path, but nothing seems to work and my code is as follows:

from pathlib import Path
from st_pages import show_pages_from_config

# Set page configuration
st.set_page_config(
    page_title="SPICE",
    layout="wide"
)

raw_path = Path() / ".streamlit" / "pages.toml"
show_pages_from_config(path=raw_path)

@Bikash I would recommend “poking around” in the container filesystem a bit in the code, see what’s there, and what’s not.

For example:


st.write(raw_path.exists())
st.write(raw_path.absolute())

st.write(Path().iterdir())

and so on, trying to figure out if that file is actually where you think it is.

Hello @blackary,

Thank you for the prompt reply.
As you said I made the following changes in the code:
image

When I deploy locally I get the following view which is correct:

When the app is deployed in cloud this is the following view which seems to me correct as well:

Let me know what you think and where can the things go wrong.

Regards,
Bikash

Hello @blackary,

Got a hold of you post regarding st.navigation.

After several tests as you said, finally got the issue resolved by using st.navigation and removing pages.toml with the below code:

pg = st.navigation(
    [
        st.Page(page=os.path.join(abs_path, "pages", "home.py"), title="Home", icon="🏛️"),
        st.Page(page=os.path.join(abs_path, "pages", "purchaseOrderMonitoring.py"), title="Purchase Order Monitoring", icon="💻"),
        st.Page(page=os.path.join(abs_path, "pages", "suppliers.py"), title="Suppliers", icon="🏅"),
        st.Page(page=os.path.join(abs_path, "pages", "standardVisualization.py"), title="Standard Visualization", icon="💹"),
        st.Page(page=os.path.join(abs_path, "pages", "aiChatAndVisualization.py"), title="AI Chat and Visualization", icon="🤖")
    ]
)
pg.run()

Also with using st.navigation, I could see the urls are standardized and no ‘%’ is used.

Many thanks for the help!

Regards,
Bikash

1 Like