The page and navigation can do this.
See the navigation below.
pg = st.navigation([
st.Page("home.py", title='Home'),
st.Page(sports, title="Sports"),
])
If sports nav is hit you can create a new sidebar nav.
pg = st.navigation([
st.Page(sports_page, title='Sports'),
st.Page(soccer, title="Soccer"),
st.Page(basketball, title="Basketball"),
])
What you need is a session variable that tracks the nav selected.
if ss.sports:
pg = st.navigation([
st.Page(sports_page, title='Sports'),
st.Page(soccer, title="Soccer"),
st.Page(basketball, title="Basketball"),
])
else:
pg = st.navigation([
st.Page("home.py", title='Home'),
st.Page(sports, title="Sports"),
])
pg.run()
Define a session variable sports.
import streamlit as st
from streamlit import session_state as ss
if 'sports' not in ss:
ss.sports = False
Once the sports nav is pressed, set the sports to true.
def sports():
ss.sports = True
st.rerun()
And that would bring us to a sports multipage app, with pages such as basketball with the ability to go back to the home.
def basketball():
st.title("Basketball")
if st.button('Back to Main Page'):
ss.sports = False
st.rerun()
Complete sample code.
app.py
import streamlit as st
from streamlit import session_state as ss
if 'sports' not in ss:
ss.sports = False
def sports():
ss.sports = True
st.rerun()
def basketball():
st.title("Basketball")
if st.button('Back to Main Page'):
ss.sports = False
st.rerun()
def soccer():
st.title("Soccer")
if st.button('Back to Main Page'):
ss.sports = False
st.rerun()
def sports_page():
st.title("Sports")
if st.button('Back to Main Page'):
ss.sports = False
st.rerun()
if ss.sports:
pg = st.navigation([
st.Page(sports_page, title='Sports'),
st.Page(soccer, title="Soccer"),
st.Page(basketball, title="Basketball"),
])
else:
pg = st.navigation([
st.Page("home.py", title='Home'),
st.Page(sports, title="Sports"),
])
pg.run()
home.py
import streamlit as st
st.title('Home')
In the beginning you have this.
After pressing Sports.
After pressing basketball.