I need the sidebar to change the pages it displays depending on the selected options

Summary

The main page has buttons that the user can click, and the sidebar adjusts its content based on the selected button. To achieve better organization, it is necessary to use ‘switch_page’ so that all elements are placed in a single section. It is important to note that the user must have logged in beforehand.

###The proposed changes are as follows:

Changes in streamlit.string_util

Code snippet:

_file_path = "pages"

def set_file_path(new_file_path):
    global _file_path
    _file_path = new_file_path

def get_pages(main_script_path_str: str) -> Dict[str, Dict[str, str]]:
    global _cached_pages
    global _file_path 
    # Avoid taking the lock if the pages cache hasn't been invalidated.
    

    with _pages_cache_lock:
        # The cache may have been repopulated while we were waiting to grab
        # the lock.


        main_script_path = Path(main_script_path_str)
        main_page_icon, main_page_name = page_icon_and_name(main_script_path)
        main_page_script_hash = calc_md5(main_script_path_str)

        # NOTE: We include the page_script_hash in the dict even though it is
        #       already used as the key because that occasionally makes things
        #       easier for us when we need to iterate over pages.
        pages = {
            main_page_script_hash: {
                "page_script_hash": main_page_script_hash,
                "page_name": main_page_name,
                "icon": main_page_icon,
                "script_path": str(main_script_path.resolve()),
            }
        }

        pages_dir = main_script_path.parent / _file_path
        
        page_scripts = sorted(
            [
                f
                for f in pages_dir.glob("*.py")
                if not f.name.startswith(".") and not f.name == "__init__.py"
            ],
            key=page_sort_key,
        )

        for script_path in page_scripts:
            script_path_str = str(script_path.resolve())
            pi, pn = page_icon_and_name(script_path)
            psh = calc_md5(script_path_str)

            pages[psh] = {
                "page_script_hash": psh,
                "page_name": pn,
                "icon": pi,
                "script_path": script_path_str,
            }

        return pages

This allows me to have the following hierarchy and change the different paths to load in the sidebar

Projecto

  • Pages
    • Subpages
    • Subpages1
    • SubpagesN

I use it in the following way

Code snippet:

from streamlit.source_util import set_file_path


 set_file_path("pages/subpages")
 switch_page("page_name")

The problem:
The problem starts when there are more than two users connected in two different paths. When the first user who entered clicks on a tab, the page not found message appears. I would like to be able to do the same but in the session to avoid this complication.

If you take a look at st_pages, there are options to configure which pages display in navigation or not. (Though as noted at the bottom of the readme, the page hiding function is CSS based so make sure to put some kind of check at the top of your restricted pages if you have a security consideration.)

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