I would recommend using built-in streamlit multipage support instead of custom functions like you’re using. There are currently two supported methods:
- Just use pages/ folder: Create a multipage app - Streamlit Docs
- Use st.page_link: Build a custom navigation menu with `st.page_link` - Streamlit Docs
For example, here’s a method using st.page_link:
nav_example.py
import streamlit as st
def add_navigation():
st.set_page_config(page_title="Nexo_admin", page_icon=":robot:", layout="wide")
st.sidebar.header("Navigation")
st.sidebar.page_link("main_page.py", label="Home", icon="🏠")
st.sidebar.page_link("pages/admin.py", label="Admin", icon="🔑")
st.sidebar.page_link("pages/chatbot.py", label="Chatbot", icon="👾")
main_page.py
import streamlit as st
from nav_example import add_navigation
add_navigation()
st.title("Main Page")
pages/admin.py
import streamlit as st
from nav_example import add_navigation
add_navigation()
st.title("Admin")
And so on.
If you then do streamlit run main_page.py
, it will run the main page, and have links in the sidebar to go to the other pages, like this