If you’re creating a debugging post, please include the following info:
-
Are you running your app locally or is it deployed? loccally
-
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. -
Share the link to your app’s public GitHub repository (including a requirements file).
-
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, thest.set_page_config
function is being called multiple times, causing errors. How can I resolve this issue? -
The application should start with
mainpage.py
as the default page when the server is started. -
The
st.set_page_config
function should be called only once to avoid errors caused by multiple calls. -
Each page (
mainpage.py
,admin.py
,chatbot.py
) should be loaded dynamically without callingst.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=“”,
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”)
- Share the Streamlit and Python versions.