How to set page config default layout to wide without calling set_page_config?

I am facing this issue when trying to set page config in a multi-page app. The multi-page app is from here.

I have an idea of setting the default layout to wide which will solve my issue but how to do so?

StreamlitAPIException : set_page_config() can only be called once per app, and must be called as the first Streamlit command in your script.

Hi @Keeb_Thock, welcome to the community!

I was facing a similar issue,
Below solved the issue is my case:

in your main.py file, add the page config to the state object.

import streamlit as st

from SessionState import _get_state


state = _get_state()

state.page_config = st.set_page_config(
    page_title="BPJV SI Database Manager test",
    layout="wide",
    initial_sidebar_state="expanded",
)

# rest of the code

state.sync()

Where did you get your SessionState? @maarten-betman
Can I have a look at the full code of SessionState? The one I used is different and even after adding your suggestion, the same error still appears.

import streamlit as st
import pandas as pd
from pages import login, signup, custom_model # import your app modules here
from db_func import *
from session_state import _get_state

# Hide mainmenu and footer
hide_streamlit_style = """
            <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
            </style>
            """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)



def main():
    state = _get_state()

    state.page_config = st.set_page_config(page_title="ABC", layout="wide", initial_sidebar_state="expanded")

    pages = {
        "Login": login.app,
        "Signup": signup.app,
        "Custom Model": custom_model.app,
    }

    st.sidebar.title(":floppy_disk: Page states")
    page = st.sidebar.selectbox("Select Page", tuple(pages.keys()))

    # Display the selected page with the session state
    pages[page](state,st)
    
    # Mandatory to avoid rollbacks with widgets, must be called at the end of your app
    state.sync()

if __name__ == "__main__":
    main()

Got it from this gist:

If the problem persists you can try to remove the __name__ == "__main__" block.
Shouldn’t be required in your main page file when you run streamlit.

@maarten-betman There is no _get_state though from the gist you linked.

Yes, must have been another gist. Can’t find the original gist.
Anyway, I just uploaded my SessionState.py as gist.

Even after I use your SessionState and removing __name__ == "__main__", I am still facing the same error.

import streamlit as st
import pandas as pd
from pages import login, signup, custom_model # import your app modules here
from db_func import *
from session_state import _get_state

# Hide mainmenu and footer
hide_streamlit_style = """
            <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
            </style>
            """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)



""" def main():
    state = _get_state()
    #state.stock_list=[]
    state(stock_list=[])
    state.page_config = st.set_page_config(page_title="WhaleBot - Bursa Robo-Advisor", layout="wide", initial_sidebar_state="expanded")

    pages = {
        "Login": login.app,
        "Signup": signup.app,
        "Custom Model": custom_model.app,
    }

    st.sidebar.title(":floppy_disk: Page states")
    page = st.sidebar.selectbox("Select Page", tuple(pages.keys()))

    # Display the selected page with the session state
    pages[page](state,st)
    
    # Mandatory to avoid rollbacks with widgets, must be called at the end of your app
    state.sync()

if __name__ == "__main__":
    main() """

state = _get_state()
#state.stock_list=[]
state(stock_list=[])
state.page_config = st.set_page_config(page_title="ABC", layout="wide", initial_sidebar_state="expanded")

pages = {
    "Login": login.app,
    "Signup": signup.app,
    "Custom Model": custom_model.app,
}

st.sidebar.title(":floppy_disk: Page states")
page = st.sidebar.selectbox("Select Page", tuple(pages.keys()))

# Display the selected page with the session state
pages[page](state,st)

# Mandatory to avoid rollbacks with widgets, must be called at the end of your app
state.sync()

My next guess will be the way you navigate between multiple pages might be different from my approach. Any idea? @maarten-betman

I follow a similar pattern, so can’t help you any further sorry.

Last things you could look at:
I call _get_state and the page_config directly after the imports.

Add the empty list to the state object like this:
state.stock_list = []

Wow, my initial code works too as long as I move the code related to hiding mainmenu and footer to the bottom. Lol… Thanks @maarten-betman for your help

1 Like