want to configure my Streamlit application so that it starts with a specific page

If you’re creating a debugging post, please include the following info:

  1. Are you running your app locally or is it deployed? loccally

  2. If your app is deployed:
    a. Is it deployed on Community Cloud or another hosting platform?
    b. Share the link to the public deployed app.

  3. Share the link to your app’s public GitHub repository (including a requirements file).

  4. Share the full text of the error message (not a screenshot).
    I want to configure my Streamlit application so that it starts with a specific page (mainpage.py) when the server is launched. Currently, the st.set_page_config function is being called multiple times, causing errors. How can I resolve this issue?

  5. The application should start with mainpage.py as the default page when the server is started.

  6. The st.set_page_config function should be called only once to avoid errors caused by multiple calls.

  7. Each page (mainpage.py, admin.py, chatbot.py) should be loaded dynamically without calling st.set_page_config again.

Here is the current setup of streamlit_app.py:
import streamlit as st
import importlib.util
import sys

Page configuration (called only once)

st.set_page_config(
page_title=“Nexo_admin”,
page_icon=“:robot:”,
layout=“wide”
)

Initialize session state

if ‘page’ not in st.session_state:
st.session_state[‘page’] = ‘mainpage’

Function to change pages

def set_page(page):
st.session_state[‘page’] = page
st.experimental_rerun() # Refresh the page on change

Page link configuration

page = st.session_state[‘page’]

Add page links to the sidebar

st.sidebar.header(“Navigation”)
st.sidebar.button(“Main Page”, on_click=lambda: set_page(‘mainpage’))
st.sidebar.button(“Admin Page”, on_click=lambda: set_page(‘admin’))
st.sidebar.button(“Chatbot Page”, on_click=lambda: set_page(‘chatbot’))

Function to load pages dynamically

def load_page(module_name, file_path):
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)

Load the default or selected page

if page == ‘mainpage’:
load_page(“mainpage”, “./pages/mainpage.py”)
elif page == ‘admin’:
load_page(“admin”, “./pages/admin.py”)
elif page == ‘chatbot’:
load_page(“chatbot”, “./pages/chatbot.py”)

  1. Share the Streamlit and Python versions.

I would recommend using built-in streamlit multipage support instead of custom functions like you’re using. There are currently two supported methods:

  1. Just use pages/ folder: Create a multipage app - Streamlit Docs
  2. 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

There is new multipage functionality available in version 1.36.0. You can declare default pages (conditionally, even) with st.navigation.

1 Like