Iām new to Streamlit and am building a Multipage App as described here Multipage apps. Rather than using the sidebar to navigate through the pages, Iād like to create a walk-through experience using a āNext Pageā button at the bottom of each page. Unfortunately, I havenāt been able to figure out how to do this, other than with a very hacky JS approach that forces a page reload. What is the proper way to do this using the new Streamlit Multipage App functionality?
A minimal example of what I want to do:
multipage_app.py
import streamlit as st
st.header("Main Page")
st.button("Button I want to click to go to next page")
We donāt have a built-in way to do this right now from Python. But you can use just a normal link via markdown or HTML. E.g. insert this into multipage_app.py:
This should add a link that navigates to the page in next_page.py. Note that I put in target="_self" here so that the link opens in the same tab. By default, links in Streamlit apps open in a new tab.
Now, this isnāt a button yet! But you can use some custom CSS to style a link like a button. Have a look through this thread. It talks about download buttons (which is not what you want) but the CSS part should be exactly the same for any kind of link.
Iāll also add your request to our internal feedback tracker for multipage apps, so we can think about a better solution for this in the future!
I have the same doubt.
Wondering if itās possible use the native streamlit multipages not just the st.markdown:
The st.markdown will open a new website em users interface. I do not want that.
Maybe itās possible use CSS to hide the st.sidebar and use āon_clickā button to call a function that change the page clicked? @andfanilo@randyzwitch
Hey jrieke, thanks for your help! Your solution works as advertised, which I appreciate, but the page reloads which kills the session state I was trying to share between the pages. Is there any way to access the pageās st.sidebar state and then set that directly on a button click?
@cbattle This is a pretty hacky solution, but should work to change the page
def switch_page(page_name: str):
from streamlit import _RerunData, _RerunException
from streamlit.source_util import get_pages
def standardize_name(name: str) -> str:
return name.lower().replace("_", " ")
page_name = standardize_name(page_name)
pages = get_pages("streamlit_app.py") # OR whatever your main page is called
for page_hash, config in pages.items():
if standardize_name(config["page_name"]) == page_name:
raise _RerunException(
_RerunData(
page_script_hash=page_hash,
page_name=page_name,
)
)
page_names = [standardize_name(config["page_name"]) for config in pages.values()]
raise ValueError(f"Could not find page {page_name}. Must be one of {page_names}")```