blackary / st_pages

An experimental version of Streamlit Multi-Page Apps
MIT License
467 stars 77 forks source link

Can't use st.switch_page with st_pages #95

Closed mgsmyth closed 7 months ago

mgsmyth commented 7 months ago

Is there a way to navigate to a specific page of the app with st_pages? I'd like to use Streamlit's st.switch_page, but it does not seem to work with st_pages.

Error message and simple example app below.

Error:

StreamlitAPIException: Could not find page: pages/details.py. Must be the file path relative to the main script, from the directory: project. Only the main app file and files in the pages/ directory are supported.

Traceback:
File "/Users/meghan/Desktop/project/pages/search.py", line 14, in <module>
    render()
File "/Users/meghan/Desktop/project/pages/search.py", line 12, in render
    st.switch_page("pages/details.py")

Simple repro:

app
 |
 +-- .streamlit/
 |  +-- pages.toml
 |
 +-- home.py
 |    
 +-- pages/
 |  +-- details.py
 |  +-- search.py

.streamlit/pages.toml:

[[pages]]
path = "home.py"
name = "Home"

[[pages]]
path = "pages/search.py"
name = "Search"
icon = ":mag:"

pages/details.py:

from st_pages import add_page_title, show_pages_from_config
import streamlit as st

add_page_title()
show_pages_from_config()

def render():
    st.title("Details")
    if st.button("Search"):
        st.switch_page("pages/search.py")

render()

pages/search.py:

from st_pages import add_page_title, show_pages_from_config
import streamlit as st

add_page_title()
show_pages_from_config()

st.session_state.claim = None

def render():
    if st.button("details"):
        st.switch_page("pages/details.py")

render()
mgsmyth commented 7 months ago

Figured this out, and posting the solution in case it's helpful for others: Omitting a page from the pages.toml config in order to hide it from the sidebar won't work if you need to use that page for app navigation. The proper way to hide the page is to call hide_pages(["<page name>"])