Option Menu as main menu


Hey all, is it possible to simulate a page change via the option menu as a menu, as well as changing the layout settings (wide/centered) this way?

Steps to reproduce

Code snippet:

# Create a sidebar for the menu
with st.sidebar:
    selected_page = option_menu("CHARLIE", list(PAGE_MAP.values()))

# Create a menu to switch between pages in the sidebar

# Use session state to track the selected page
if "page" not in st.session_state:
    st.session_state.page = "main_dashboard.py"

# Update the selected page based on the user's choice
if selected_page != PAGE_MAP[st.session_state.page]:
    for file, name in PAGE_MAP.items():
        if selected_page == name:
            st.session_state.page = file

# Import and run the selected page from the "pages" folder
selected_page_path = os.path.join("app_pages", st.session_state.page)
with open(selected_page_path, "r", encoding="utf-8") as selected_page_file:
    code = selected_page_file.read()

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

I would love include links in the python pages to switch between the pages, similar to clicking on the menu.

I tried doing that with this code:

    if st.button("Go to Review Page"):
        st.session_state.page = "review.py"  # Change to the Review page

but it won’t work

I’m sure I’m missing the syntax or similar.

In the same context, I would love to have each .py page behave with a different layout, assuming that should work but I wasn’t able to do it

Thanks a lot in advance

Hey @ghostmize,

Thanks for sharing this question! Check out the Streamlit extras package, which features the functionality to switch pages programmatically.

Hey Caroline,

I’ve seen the component but not 100% sure yet how to implement it, since I’m not using the default MPA (I did not want the sidebar with the pages to appear by default)

So I changed my pages folder name to app_pages.
Is there a way to edit extra components to fit my structure?
Or, you suggest I just copy the code from the extras page.

Thanks a lot

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